ruby-on-rails - Rails 正在向后路由我的 calendar_event_path 吗?

标签 ruby-on-rails ruby ruby-on-rails-3 routing

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/

相关文章:

ruby-on-rails - Ruby Integer()、Array() 等——它们是什么?他们来自哪里?

ruby-on-rails - RVM:无法安装 ruby

ruby-on-rails - ruby rails : how to migrate changes made on models?

ruby - ruby 中的条件链接

ruby-on-rails - WEBrick::HTTPStatus::LengthRequired 与 Twilio

ruby-on-rails - 邪恶_pdf : Is it possible to have the header only show on the first page & the footer only on the last?

ruby-on-rails - 事件模型序列化器 respond_with 导致未定义的方法 "to_model"?

ruby-on-rails - 来自 cucumber 的 HTTP POST XML 内容

ruby-on-rails - 如何在 Ruby on Rails 中不使用 devise 添加新的管理员用户

ruby-on-rails-3 - Rails 4.0.0.0 俄罗斯娃娃缓存与belongs_to 模型