hol*_*den 3 ruby routing routes nested ruby-on-rails
我有一个像这样的单一嵌套资源:
map.resources :bookings, :member => { :rate => :post } do |booking|
booking.resource :review
end
Run Code Online (Sandbox Code Playgroud)
给我这些路线:
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"}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
<% form_for [@booking, @review] do |f| %>
Run Code Online (Sandbox Code Playgroud)
哪个返回了这个错误:
undefined method `booking_reviews_path' for #<ActionView::Base:0x10572b888>
Run Code Online (Sandbox Code Playgroud)
用我的评论控制器
def new
@review = Review.new
@booking = Booking.find(params[:booking_id])
end
Run Code Online (Sandbox Code Playgroud)
但这有效...如果我明确列出了URL ...
<% form_for :review, :url => booking_review_path(@booking) do |f| %>
Run Code Online (Sandbox Code Playgroud)
这不是什么大问题......但我想知道我做错了什么.
谢谢
我认为form_for假设所有嵌套资源都是多个资源.修复form_for将是正确的事情(我敦促你提交一个错误),但同时,你可以伪造它:
# in app/helpers/application_helper.rb
module ApplicationHelper
def booking_reviews_path(*args)
booking_review_path(*args)
end
end
Run Code Online (Sandbox Code Playgroud)
您无法使用,alias_method因为该booking_review_path模块中不存在该方法,但这基本上是相同的.
| 归档时间: |
|
| 查看次数: |
1643 次 |
| 最近记录: |