Nested form for a unique resource

I have a special nested resource, for example:

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

Providing me with the following 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 the controller

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

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

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

It doesn't matter ... but I wonder what I'm doing wrong.

thank

+3
source share
1 answer

I think that form_forassumes that all the resources invested are multiple resources. Committing form_forwould be the right thing (and I urge you to send an error ), but at the same time you can fake this:

# in app/helpers/application_helper.rb
module ApplicationHelper

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

end

alias_method, booking_review_path , .

+8

Source: https://habr.com/ru/post/1732760/


All Articles