ruby-on-rails - 嵌套 form_for 单一资源

标签 ruby-on-rails ruby routing nested routes

我有一个像这样的单一嵌套资源:

  map.resources :bookings, :member => { :rate => :post } do |booking|
    booking.resource :review
  end

给我这些路线:

   new_booking_review GET    /bookings/:booking_id/review/new(.:format)      {:controller=>"reviews", :action=>"new"}
  edit_booking_review GET    /bookings/:booking_id/review/edit(.:format)     {:controller=>"reviews", :action=>"edit"}
       booking_review GET    /bookings/:booking_id/review(.:format)          {:controller=>"reviews", :action=>"show"}
                      PUT    /bookings/:booking_id/review(.:format)          {:controller=>"reviews", :action=>"update"}
                      DELETE /bookings/:booking_id/review(.:format)          {:controller=>"reviews", :action=>"destroy"}
                      POST   /bookings/:booking_id/review(.:format)          {:controller=>"reviews", :action=>"create"}

我试过这个:

<% form_for [@booking, @review] do |f| %>

返回此错误:

undefined method `booking_reviews_path' for #<ActionView::Base:0x10572b888>

与我的评论 Controller

  def new
    @review = Review.new
    @booking = Booking.find(params[:booking_id])
  end

但这行得通...如果我明确列出 URL...

<% form_for :review, :url => booking_review_path(@booking) do |f| %>

这没什么大不了的......但我想知道我做错了什么。

谢谢

最佳答案

我认为 form_for 假定所有嵌套资源都是复数 资源。修复 form_for 将是正确的 事情(我敦促你 submit a bug ),但与此同时,你可以伪造它:

# in app/helpers/application_helper.rb
module ApplicationHelper

  def booking_reviews_path(*args)
    booking_review_path(*args)
  end

end

您不能使用 alias_method,因为 booking_review_path 方法在该模块中不存在,但这本质上是一样的。

关于ruby-on-rails - 嵌套 form_for 单一资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2261827/

相关文章:

Asp.Net 路由到通用处理程序

url - symfony2 使用正则表达式为单个 Controller 操作使用多个 url 模式

ruby-on-rails - 为什么我的夹具对象在我的 Rails Test::Unit 测试中不可用?

ruby-on-rails - 免费的 Rails 3 脚手架模板?

ruby-on-rails - 如何将 ActiveRecord 表名转换为模型类名

javascript - Rails 应用程序 C9 中的 JavaScript 问题

javascript - Rails,将 ruby​​ 参数添加到 javascript

arrays - 如何从数组中获取第一个匹配项

ruby - 如何将 'each' 方法添加到 Ruby 对象(或者我应该扩展数组)?

php - 如果尝试进入登录屏幕,Laravel 5 会重定向到主页