I’m trying to get the hang of basic Rails routing.
I have a model called page which I generated with a scaffold.
I have added a method called addchild which I would like to access through
'pages/addchild/:id'
So far so good. However, I want to set up a link to this method like so:
<%= link_to 'Add child page', addchild_page_path(page) %>
Passing the ID of the current page as a parameter.
When I load my index view (where the link is), I get the following message:
undefined local variable or method `addchild_page_path' for #<ActionView::Base:0xb67797d0>
Have I misunderstood how the path/link_to method works?
My routes file looks like this:
map.resources :pages
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Any advice would be greatly appreciated.
Thanks.
You need to add a route to it to be able to use the named path methods.
Since you mentioned you used scaffolding, you probably have the route setup as a resource, so all that you need to do is add the method:
Would give you an
addchild_pages_path(and the actual created path would look like/pages/:id/addchildYou then use it like this:
addchild_pages_path page, don’t call theidmethod directly since it is not resourceful (you won’t use theto_paramin the page class, which you might want to do later).If you really want the url to show up as
/pages/addchild/:id(which I don’t recommend) you can addbefore the
map.resources :pagesrow in your routes.rb, and then use the path method as above.