本文介绍了嵌套的form_for单个资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单一的嵌套资源,如下所示:

  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| %>

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

谢谢

我认为form_for假定所有嵌套资源都是 plural 资源.修复form_for将是正确的工作(我敦促您提交错误),但与此同时,您可以伪造它:

# in app/helpers/application_helper.rb
module ApplicationHelper

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

end

您不能使用alias_method,因为该模块中不存在方法booking_review_path,但这本质上是同一件事.

I have a singular nested resource like so:

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

Giving me these routes:

   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"}

I tried this:

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

Which returned this error:

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

With my reviews controller

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

But this works... if I list the URL explicitly...

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

It's not a big deal... but i'm wondering what i'm doing wrong.

Thanks

解决方案

I think form_for assumes all nested resources are plural resources. Fixing form_for would be the correct thing to do (and I urge you to submit a bug), but in the mean time, you can fake it:

# in app/helpers/application_helper.rb
module ApplicationHelper

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

end

You can't use alias_method because the method booking_review_path doesn't exist in that module, but this is essentially the same thing.

这篇关于嵌套的form_for单个资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:26