Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails is routing my calendar_event_path backwards?

Rails is giving me urls that have the ids in the wrong order. It's handing the parent id to the child and the child id to the parent.

In my routes.rb I have

resources :calendars do
  resources :events
end

which rake routes tells me is setting up good routes, e.g.

calendar_event GET    /calendars/:calendar_id/events/:id(.:format)      {:controller=>"events", :action=>"show"}

So when I ask for the path for that route from my view partial,

<%= link_to "#{event.name} @ #{event.calendar.name} #{event.calendar.year}", calendar_event_path(event) %>

it gives me a url that has the ids reversed...

http://localhost:3000/calendars/<-eventid->/events/<-calendarid->

Any help would be appreciated!!!

Edit: in my model, Event belongs_to :calendar and Calendar has_many :events. I'm also using Mongoid as my ORM, not ActiveRecord, though I hope that isn't the issue.

like image 275
Kenny Winker Avatar asked Dec 19 '25 22:12

Kenny Winker


1 Answers

When you use the nested resource's URL helper calendar_event_path you need to provide it the calendar object (or ID) too, eg:

 calendar_event_path(event.calendar, event)

An alternative is:

 <%= link_to "#{event.name} @ #{event.calendar.name} #{event.calendar.year}", [event.calendar, event] %>
like image 167
Jits Avatar answered Dec 22 '25 15:12

Jits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!