willoller
willoller

Reputation: 7330

Rails: using nested named routes

routes.rb:

resources :jobs do
  resources :activitylogs
end

rake routes:

...
                     POST /jobs/:job_id/activitylogs(.:format)          {:controller=>"activitylogs", :action=>"create"}
new_job_activitylog  GET  /jobs/:job_id/activitylogs/new(.:format)      {:controller=>"activitylogs", :action=>"new"}
edit_job_activitylog GET  /jobs/:job_id/activitylogs/:id/edit(.:format) {:controller=>"activitylogs", :action=>"edit"}
...

How do I use the route new_job_activitylog?

Doing <%= new_job_activitylog %> gives undefined exception - so does using link_to which most of the examples I see are using.

Upvotes: 1

Views: 279

Answers (2)

Dogbert
Dogbert

Reputation: 222388

Use

<%= new_job_activitylog_path %>

or

<%= new_job_activitylog_url %>

_path returns a relative path, while _url returns a complete url including http://domain.com if you've set it in your config.

Upvotes: 4

willoller
willoller

Reputation: 7330

To use those route names, I just had to append _path to them.

So: new_job_activitylog is undefined, but new_job_activitylog_path is a method in the view that takes the job id as a parameter.

<%= link_to 'new', new_job_activitylog_path(:job_id => @job.id) %>

works!

Upvotes: 0

Related Questions