Ruby on rails Rails 5错误接受多态中的\u嵌套\u属性\u

Ruby on rails Rails 5错误接受多态中的\u嵌套\u属性\u,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我在尝试创建产品时出错:“无法构建关联‘type_data’。是否正在尝试构建多态一对一关联?” 我不知道发生了什么事。我看到了许多使用相同代码的示例,但我的示例不起作用 这里是我的product.rb class Product < ApplicationRecord belongs_to :type_data, polymorphic: true accepts_nested_attributes_for :type_data, allow_destroy: true end

我在尝试创建产品时出错:“无法构建关联‘type_data’。是否正在尝试构建多态一对一关联?”

我不知道发生了什么事。我看到了许多使用相同代码的示例,但我的示例不起作用

这里是我的
product.rb

class Product < ApplicationRecord
  belongs_to :type_data, polymorphic: true
  accepts_nested_attributes_for :type_data, allow_destroy: true
end
类产品
这是我的酒店房间

class HotelRoom < ApplicationRecord
  has_one :product, as: :type_data, dependent: :destroy
  has_many :rates
  accepts_nested_attributes_for :rates, allow_destroy: true
end
class HotelRoom
我的产品/_form.html.erb

<%= f.fields_for :type_data do |builder| %>
  <%= render "edit_hotel_account_fields", f: builder %>
<% end %>

我的产品/\u编辑\u帐户\u字段.heml.erb

<%= f.fields_for :rates do |builder| %>
  <%= render 'rate_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add rate", f, :rates %>

我的产品/_rate_fields.html.erb

<div class="form-group">
  <label><%= t('products.new.rates.double_price') %></label>
  <%= f.number_field :double_price, step: 0.1, class: 'form-control' %>
</div>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>

这是我所有的产品

    class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to products_path, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  # AJAX cargas más fechas
  def date_range
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:status, :provider_id, :commission, :title, :commercial_title, :title_seo, :description, :seo_description, :slug_web, :slug_redirection, :services, :info, :province, :state, :city, :country, :cancel_conditions, :limit_children_age, :special_alert, :notes, :stock, :iva, :minimum_advance, type_data_attributes: [:room_stock, :room_service, :_destroy, rates_attributes: [:title, :start_date, :end_date, :hotel_room_id, :individual_price, :double_price, :child_bed_price, :adult_bed_price, :adult_breakfast_price, :adult_medium_price, :adult_complete_price, :child_breakfast_price, :child_medium_price, :child_complete_price, :_destroy]], dates: {}, map: {}, address_autocomplete: {})
    end
end
class ProductsController
有人知道发生了什么事吗


谢谢!

因为酒店模型有一款产品是通过多态的

class HotelRoom < ApplicationRecord
  has_one :product, as: :type_data, dependent: :destroy
  accepts_nested_attributes_for :product, allow_destroy: true
  accepts_nested_attributes_for :rates, allow_destroy: true
 ....
end
以酒店视图形式显示

<%= form_for(@hotel_room) do |f| %>
   ....
   <%= f.fields_for(:product) do |p| %>
    ....
   <% end %>
<% end %>

....
....
注:
如果您在HotelRoom和Product之间有共同的数据,那么您需要引入另一个模型,如TypeData,并将其设置为多态。

您可以发布
products\u controller
代码吗?是的,使用
products\u controller.rb中的所有代码进行编辑。请查看此链接,效果非常好!谢谢!!!
<%= form_for(@hotel_room) do |f| %>
   ....
   <%= f.fields_for(:product) do |p| %>
    ....
   <% end %>
<% end %>