Rails 5错误accept_nested_attributes_for多态

时间:2017-07-12 12:55:40

标签: 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

这是我的hotel_room.rb

class HotelRoom < ApplicationRecord
  has_one :product, as: :type_data, dependent: :destroy
  has_many :rates
  accepts_nested_attributes_for :rates, allow_destroy: true
end

我的产品/ _form.html.erb

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

我的产品/ _edit_account_fields.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" %>

这就是我的所有products_controller.rb

    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

有谁知道发生了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

由于酒店模型有一个产品通过多态,

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

在hotel_rooms控制器新动作中

def new
    @hotel_room  = HotelRoom.new
    @hotel_room.build_product
end

在酒店视图表格中

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

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