问题
I am trying to create an associated polymorphic object through fields_for.
I am getting an unknown attribute error though when the parent is created, are the @order params not getting through somehow?
Parent/Order controller -
# 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 -
<%= form_for(@order) do |f| %>
<% if @order.errors.any? %>
<% end %>
<%= f.fields_for :orthosis_specification do |fa| %>
Order Model -
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
Full error -
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"}
Orthosis Spec Model -
class OrthosisSpecification < ActiveRecord::Base
has_one :order, :as => :product, class_name: 'Order'
accepts_nested_attributes_for :order
end
Thanks for any help!
来源:https://stackoverflow.com/questions/33522095/unknown-attribute-on-polymorphic-nested-form-object-creation