【问题标题】:nested form_for singular resource嵌套 form_for 单一资源
【发布时间】:2011-01-16 17:42:06
【问题描述】:

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

  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>

使用我的评论控制器

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

但这有效...如果我明确列出 URL...

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

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

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby routing nested routes


    【解决方案1】:

    我认为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 方法,但这本质上是一回事。

    【讨论】:

    猜你喜欢
    • 2011-01-03
    • 2017-01-24
    • 2012-07-08
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多