无法呈现(双重)嵌套形式

Unable to render (double) nested form

我有 3 个模型,Classses,ClassPrices 和 Bookings。 ClassPrices 属于 Classses & Bookings 属于 ClassPrices。我在 ClassPrices 页面上呈现预订表格时遇到问题。

1. routes.rb

resources :classses do
  resources :class_prices do
    resources :bookings
  end
end

2。 class_price.rb

class ClassPrice < ApplicationRecord
  belongs_to :classs
  has_many :bookings
end

3。 bookings.rb

class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :class_price
end

4。 bookings_controller.rb

def new
  @booking = Booking.new
end    

我使用的是简单表单 gem,由于我的代码量有限,这是我表单的 header:

<%= simple_form_for [@classs.class_prices, @class_price.bookings.new] do |f| %>

我得到的错误是:

undefined method `to_model' for #<ClassPrice::ActiveRecord_Associations_CollectionProxy:0x00007fcf01a70ff8>
Did you mean?  to_xml

编辑:

5. class_prices_controller.rb

def show
  @classs = Classs.find(params[:classs_id])
  @class_price = ClassPrice.find(params[:id])
  @booking = Booking.new
  @classs.class_prices.find(params[:id]).bookings.create(booking_params)
end

undefined method `to_model' for ClassPrice::ActiveRecord_Associations_CollectionProxy:0x00007fcf01a70ff8

@classs.class_prices returns 集合和 第一个参数 simple_form_for(或任何其他形式的助手)应该 从不成为合集。我觉得应该是

<%= simple_form_for [@class_price, @class_price.bookings.new] do |f| %>

更新:

根据您的路线,应该是

<%= simple_form_for [@classs, @class_price, @booking] do |f| %>