views:

18

answers:

1

Hi, 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.

+2  A: 

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:

map.resources :pages, :member => {:addchild => :get}

Would give you an addchild_pages_path (and the actual created path would look like /pages/:id/addchild

You then use it like this: addchild_pages_path page, don't call the id method directly since it is not resourceful (you won't use the to_param in 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 add

map.addchild_page "/pages/addchild/:id", :controller => :pages, :method => :addchild

before the map.resources :pages row in your routes.rb, and then use the path method as above.

Jimmy Stenke
Thanks.Yes, I wanted the url formatted the default way, I just mistyped it.
Dan