1

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.

uechan
  • 167
  • 8

1 Answers1

1

You can set that like:

<%= link_to 'Back', site_visits_path(:site_id => @site.id) %>
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • Gave it a shot, got this error: `undefined method 'id' for nil:NilClass` I think the way I'm setting `@site` in my controller must be changed somehow as the errors I'm getting are saying my `@site` is nil. `@site = Site.find(params[:site_id])` works when I use full deep nesting but in this shallow nesting case it doesn't seem to work. Thanks for the help though I'll give you a checkmark if nothing else comes up since I can't upvote yet. – uechan Jan 15 '15 at 00:09
  • I figured it out. There is no `:site_id` in my URL when using shallow nesting because the page is at `localhost:3000/visits/m`, rather than the usual `localhost:3000/sites/n/visits/m`. Thus in the `show` action of my controller, I first set `@visit = Visit.find(params[:id])` then `@site = @visit.site` to get back to the index of visits related to that Site! – uechan Jan 15 '15 at 00:30