嵌套 form_for 单一资源 [英] nested form_for singular resource
问题描述
我有一个像这样的单一嵌套资源:
I have a singular nested resource like so:
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...
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.
谢谢
推荐答案
我认为 form_for
假定所有嵌套资源都是 复数 资源.修复form_for
将是正确 的事情(我敦促您提交错误),但与此同时,您可以伪造它:
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
您不能使用alias_method
,因为该模块中不存在方法booking_review_path
,但这本质上是相同的.
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 单一资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!