请帮助解决这个问题
ActiveModel::UnknownAttributeError (unknown attribute 'order_itens_attributes' for Order.):
控制器中的mi代码
def create
@order = Order.new(order_params)
if @order.save
render json: @order, include: [:user,:order_itens], status: :created, location: @order
else
render json: @order.errors, status: :unprocessable_entity
end
end
private
def order_params
params.require(:order).permit(
:user_id, :subsidiarie_id, :amount, :start_time,
:arrive_time, :delivered_time, :cancel_time,
order_itens_attributes: [:quantity])
end
我的模型order.rb
class Order < ApplicationRecord
belongs_to :user
belongs_to :subsidiarie, optional: true
has_many :order_itens
end
model order_iten.rb
class OrderIten < ApplicationRecord
belongs_to :order, optional: true
belongs_to :product, optional: true
end
和我的帖子请求
{
"order":
{
"subsidiarie_id":1,
"user_id":1,
"amount":220,
"order_itens_attributes":
[
{
"quantity":22
}
]
}
}
似乎这是order_itens_attributes的一个问题,但我不知道它的问题
答案 0 :(得分:2)
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html在这里阅读它基本上你需要设置
accepts_nested_attributes_for :order_itens
在您的订单型号上
答案 1 :(得分:2)
在order.rb
添加
accepts_nested_attributes_for :order_itens