多态嵌套表单对象创建的未知属性

时间:2015-11-04 12:34:48

标签: ruby-on-rails polymorphic-associations fields-for

我正在尝试通过fields_for创建一个关联的多态对象。

虽然在创建父级时,我收到了一个未知的属性错误,但@order参数是否无法以某种方式通过?

父/订单控制器 -

# GET /orders/new
  def new
    @order = Order.new
    @order.build_patient
    @order.product = OrthosisSpecification.new
  end

# GET /hotels/1/edit
  def edit
    @order.build_patient
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)

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

  private

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
        params.require(:order).permit!
    end

表格 -

  <%= form_for(@order) do |f| %>
  <% if @order.errors.any? %>
  <% end %>

<%= f.fields_for :orthosis_specification do |fa| %>

订单型号 -

class Order < ActiveRecord::Base
  has_one :patient
  belongs_to :product, polymorphic: true
  accepts_nested_attributes_for :patient,
                                reject_if: proc { |attributes| attributes['first_name'].blank? },
                                allow_destroy: true
  accepts_nested_attributes_for :product


end

完整错误 -

unknown attribute 'orthosis_specification' for Order.

order_params -

>> order_params
=> {"patient_attributes"=>{"first_name"=>"", "last_name"=>"", "dob"=>"", "sex"=>"1", "diagnosis"=>"1", "posterior_flattening"=>"1", "circumference"=>"", "ap"=>"", "ml"=>"", "ci"=>"", "frontal_area"=>"1", "parietal_lateral"=>"1", "ear_position"=>"1"}, "orthosis_specification"=>{"transfer_name"=>"1", "modifications"=>"1", "primary_mods"=>"1", "top_opening"=>"1", "side_opening"=>"1", "chape_position"=>"1"}, "order_date"=>"", "date_required"=>""}

params -

authenticity_token"=>"v6gVARW+vnApuZpit6IW81ouytlWWQa1Orb4kymseESZmgiFI4KYhOrYyRQuu7yn3mijRp9m7LlFxQlMq7OaAA==", "order"=>{"patient_attributes"=>{"first_name"=>"", "last_name"=>"", "dob"=>"", "sex"=>"1", "diagnosis"=>"1", "posterior_flattening"=>"1", "circumference"=>"", "ap"=>"", "ml"=>"", "ci"=>"", "frontal_area"=>"1", "parietal_lateral"=>"1", "ear_position"=>"1"}, "orthosis_specification"=>{"transfer_name"=>"1", "modifications"=>"1", "primary_mods"=>"1", "top_opening"=>"1", "side_opening"=>"1", "chape_position"=>"1"}, "order_date"=>"", "date_required"=>""}, "commit"=>"Create Order", "controller"=>"orders", "action"=>"create"}

矫形器规格模型 -

class OrthosisSpecification < ActiveRecord::Base
    has_one :order, :as => :product, class_name: 'Order'
    accepts_nested_attributes_for :order
end

感谢您的帮助!

0 个答案:

没有答案