开发者

nested form_for singular resource

开发者 https://www.devze.com 2022-12-20 16:15 出处:网络
I have a s开发者_运维问答ingular nested resource like so: map.resources :bookings, :member => { :rate => :post } do |booking|

I have a s开发者_运维问答ingular 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消