[英]Rails 4 Unpermitted parameters: products
I need to save an array into my order.rb
model. 我需要将数组保存到我的order.rb
模型中。
The array is: params[:products]
数组是: params[:products]
The array is giving me something like this: 该数组给我这样的东西:
[{"'name'"=>"31 DVIE33N - Traditional ", "'id'"=>"2", "'quantity'"=>"1", "'accessory'"=>{"'id'"=>"7", "'name'"=>"31-SK4BLANKD-2"}}]
Create action: 创建动作:
def create
@order = Order.new(order_params)
respond_to do |format|
if @order.save
format.html { redirect_to admin_orders_path(@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
My orders params. 我的订单参数。
private
def order_params
params.permit({:products=>[], products:[])
end
I'm trying two different ways to permite products, that why you will see two arrays above 我正在尝试两种不同的方式来许可产品,这就是为什么您会在上面看到两个数组的原因
Please take a look, I'm not using somethig like: 请看看,我没有使用somethig:
params.require(:order).permit(:products => []}, :products=>[])
because if I use that I get the error: 因为如果使用它,我会得到错误:
ActionController::ParameterMissing - param is missing or the value is empty: order:
Thank you. 谢谢。
You need to do it in a Rails way... 您需要以一种Rails的方式来做...
In models/order.rb 在models / order.rb中
class Order < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products, allow_destroy: true
end
In orders_controller.rb 在orders_controller.rb中
def order_params
params.require(:order).permit(products_attributes: [:name, :etc])
end
In products_attributes
array you can pass the product attributes you want to permit. 在products_attributes
数组中,您可以传递要允许的产品属性。
You need to send product_attributes
like this: {“order”=>{“products_attributes"=>[{“name”=>”product 1”}, {“name”=>”product 2”}]}}
您需要像这样发送product_attributes
: {“order”=>{“products_attributes"=>[{“name”=>”product 1”}, {“name”=>”product 2”}]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.