Search code examples
ruby-on-railsnested-resources

Rails 4 shallow nesting UrlGenerationError, missing required keys


I'm trying shallow nested resources for the first time and having a little trouble with one my index routes.

routes.rb

resources :sites, shallow: true do
  resources :visits
end

The error I get is in my visits#show page's back button:

<%= link_to 'Back', site_visits_path(@site) %>

No route matches {:action=>"index", :controller=>"visits", :site_id=>nil} missing required keys: [:site_id]

In the index action of my VisitsController I set@site as follows:

@site = Site.find(params[:site_id])

However it's saying my :site_id is nil and I'm not sure how to set this correctly.


Solution

  • You can set that like:

    <%= link_to 'Back', site_visits_path(:site_id => @site.id) %>