0

I'm working on a basic Rails app. To get things going, I created two scaffolds.

  • calendar
  • content_items

I then created the proper associations.

app/models/calendar.rb

class Calendar < ActiveRecord::Base
   has_many :content_items
end

app/models/content_item.rb

class ContentItem < ActiveRecord::Base
  belongs_to :calendar
end

routes.rb

resources :calendars do
  resources :content_items
end

However, now when I try to view the content_items of a specific calendar, I get the following error:

ActionController::UrlGenerationError - No route matches {:action=>"show", :calendar_id=>nil, :controller=>"content_items", :id=>"5"} missing required keys: [:calendar_id]

It says the error is coming from: views/content_items/index.html.erb

<td><%= link_to 'Show', calendar_content_item_path(content_item.calendar, content_item) %></td>

I've tried a few different routes, but they lead to different errors. Do I need to update the model and/or controllers since I created the nested routes?

JeremyE
  • 1,368
  • 4
  • 20
  • 40
  • basically rails is unable to generate a url from only content_item object. as it is nested, you also need to pass parent calender object. check [documentation](http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects) – Alper Karapınar May 25 '15 at 08:25

2 Answers2

2

Try using

<td><%= link_to 'Show', calendar_content_item_path(content_item.calendar, content_item) %></td>

Amit Badheka
  • 2,677
  • 4
  • 19
  • 29
0

You forget to add _path suffix to route:

<td><%= link_to 'Show', content_items_path(calendar) %></td>