这个真的很烦人,我可能只是错过了一些愚蠢的东西。现在已经2小时了,可以看出问题是什么。错误:ActiveModel :: ForbiddenAttributesError:
order.rb
has_many :item_lines
accepts_nested_attributes_for :item_lines
order_controller.rb
def create
if params[:v1_order][:item_lines_attributes].present?
puts "attributes: "
puts params[:v1_order][:item_lines_attributes] # {"quantity"=>"1", "net_price"=>"9.99"}
@v1_order = V1::Order.new(v1_order_params)
@v1_order.item_lines.build(params[:v1_order][:item_lines_attributes])
else
@v1_order = V1::Order.new(v1_order_params)
end
end
def v1_order_params
params.require(:v1_order).permit(:state, :vat, :order_date, :user_id,
:item_lines_attributes => [:quantity, :net_price])
end
order_controller_spec.rb
expect {
post :create,
:v1_order => {vat: 20, user_id: 1,state: 0,
:item_lines_attributes => [quantity: 2, net_price: 3]},
token: @user.api_key.token
}.to change(V1::Order, :count).by(1)
**below should also be valid, but no go **
post :create,
v1_order: FactoryGirl.attributes_for(:v1_order,
item_lines_attributes: FactoryGirl.attributes_for(:v1_item_line)),
工厂
factory :v1_order, :class => 'V1::Order' do
state 0
vat 20
user_id 1
end
factory :v1_item_line, :class => 'V1::ItemLine' do
quantity 1
net_price "9.99"
end
继续这样做:
Failure/Error: post :create,
ActiveModel::ForbiddenAttributesError:
ActiveModel::ForbiddenAttributesError
答案 0 :(得分:0)
在私有函数中定义def v1_order_params。
私人 def v1_order_params .... 结束 端
答案 1 :(得分:0)
在您的控制器中,您可以尝试更改:
@ v1_order.item_lines.build(PARAMS [:v1_order] [:item_lines_attributes])
要:
@ v1_order.item_lines.build(v1_item_lines_params)