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 Routes to Create Actions View for New Pages

Why page but new_page_path

In the index.html.erb we use <%= link_to page.title, page %> to link to the page.

But to link to the form, we use <%= link_to 'New Page', new_page_path %> Wy do we need to add _path and don't use the alias new_page (from the routes.rb)

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

page is a variable holding a Page object. When it receives an object as the second argument like this, I believe link_to will call page_path(page) to get a path to the show action for that particular page, and use that as the link path.

new_page in the routes.rb file sets up the route name. One of the methods that naming the route creates is called new_page_path; I don't believe it creates any method named new_page.

Generally speaking, you want to use one of these as the second argument to link_to:

  • A path string
  • A model object that will be used to create a path string

And that's what the *_path methods return: a path string.

Documentation for link_to