Rails 给我的 URL 的 ID 顺序错误。它将父 ID 传递给子代,将子 ID 传递给父代。
在我的 routes.rb 中有
resources :calendars do
resources :events
end
哪个 rake routes
告诉我正在设置好的路线,例如
calendar_event GET /calendars/:calendar_id/events/:id(.:format) {:controller=>"events", :action=>"show"}
所以当我从我的部分 View 中请求该路线的路径时,
<%= link_to "#{event.name} @ #{event.calendar.name} #{event.calendar.year}", calendar_event_path(event) %>
它给了我一个 id 颠倒的 url...
http://localhost:3000/calendars/<-eventid->/events/<-calendarid->
任何帮助将不胜感激!!!
编辑:在我的模型中,事件 belongs_to :calendar
和日历 has_many :events
。我还使用 Mongoid 作为我的 ORM,而不是 ActiveRecord,但我希望这不是问题所在。
最佳答案
当您使用嵌套资源的 URL 帮助程序 calendar_event_path
时,您还需要为其提供 calendar
对象(或 ID),例如:
calendar_event_path(event.calendar, event)
另一种方法是:
<%= link_to "#{event.name} @ #{event.calendar.name} #{event.calendar.year}", [event.calendar, event] %>
关于ruby-on-rails - Rails 正在向后路由我的 calendar_event_path 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171680/