Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources A Route to a Read Action Finding a Page

Quynh To Tuan
Quynh To Tuan
8,192 Points

"render text:" shows error

The "render text:" in #show does not work. I get the error message: "Missing template pages/show". Why is it still looking for a template although we are using "render text"?

This is the code snippet:

def show
    render text: params[:id]
  end
Jay McGavren
Jay McGavren
Treehouse Teacher

That's strange; I would expect it to work. I wonder if your routes are set up correctly, or if there's another problem.

Can you go to your terminal, clear all text (or hit Enter a few times to mark so there's a gap after the existing text), make another browser request for localhost:3000/pages/4, and then copy and paste all new text that appears in the Rails log here?

Oliver Duncan
Oliver Duncan
16,642 Points

I think what you're looking for is render plain: params[:id]

I think the issue has to do with the version of rails one is running -- I'm locally (localhost) running 5.1.1 and I got the same error. Switching out render text with render plain worked.

However, render text works fine in my remote environment which is using rails 4.2.5.
If Jay was using rails 5 when this video was made, maybe the latest version of rail's (5.1.1)caused the issue...?

5 Answers

Quynh To Tuan
Quynh To Tuan
8,192 Points

Thank you! Using "plain" instead of "text" worked for me!

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

yvesramos I'll update the teacher's notes to mention plain.

Both text and plain work in Rails 5.0, which is the recommended version for this course. It's important to use the recommended versions of libraries when following along with tutorials. This is just one example of the incompatibilities that can happen when the versions differ.

You can install Rails 5.0 with this command:

gem install rails --version 5.0.0

As I write, this installs Rails 5.0.4, which is okay because (unlike 5.1.2) its API is still fully compatible with 5.0.0.

rails new uses the newest version of Rails installed on your system by default. If you already have a more recent version of Rails (such as 5.1) installed on your system, and you don't want to uninstall it, you'll need to ensure that you're using 5.0.0 when generating a new app. Use this command:

rails _5.0.0_ new blog

Once you change into your app's directory, as long as you're running commands using bin/rails (which is what we suggest in the course), it should use the version of Rails that the app was generated with.

yvesramos
yvesramos
12,816 Points

Thanks, Zia Khan. It worked with plain:. For those having the same issue and wonder why we can not use text: it is because is deprecated. This link might help(https://apidock.com/rails/ActionController/Base/render). I am not sure, but I think they removed it in rails 4.2. Jay, you should test it and add it in the teacher notes.

Olivier Vanbiervliet
Olivier Vanbiervliet
11,464 Points

Had the same problem. Should be mentioned below the video imho.

Vladislav Trotsenko
Vladislav Trotsenko
9,291 Points

With Rails 5.1.4 I have used:

def show
    @page = Page.find(params[:id])
    render plain: @page.title
end

or you can use render text: @page.title, but you should create app/views/pages/show.html.erb if you don't want to get 'Template is missing' error :)