Ruby on rails 具有允许的_属性的强参数(在Solidus中)
我正在使用Solidus电子商务gem for Ruby on Rails。我正在创建一个用于发送邮件的电子商务网站,因此我需要能够将地址附加到各个行项目,而不仅仅是用户的账单和发货地址。为了实现这一点,我在cart表单中传递地址参数 我得到了不允许的参数:address\u属性。以下是我通过表单传递的参数: 参数 下面是我访问它们的方式:address\u attributes=order\u params[:address\u attributes] 以下是Solidus附带的order_params方法: orders_controller.rb 我不知道为什么它使用这个if-else逻辑而不是params.require,但这不是我的代码。当我将调试器放入其中,并查看允许的\u顺序\u属性时,我看到的是:Ruby on rails 具有允许的_属性的强参数(在Solidus中),ruby-on-rails,solidus,Ruby On Rails,Solidus,我正在使用Solidus电子商务gem for Ruby on Rails。我正在创建一个用于发送邮件的电子商务网站,因此我需要能够将地址附加到各个行项目,而不仅仅是用户的账单和发货地址。为了实现这一点,我在cart表单中传递地址参数 我得到了不允许的参数:address\u属性。以下是我通过表单传递的参数: 参数 下面是我访问它们的方式:address\u attributes=order\u params[:address\u attributes] 以下是Solidus附带的order_p
[:coupon_code, :email, :special_instructions, :use_billing, {:bill_address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}], :ship_address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}], :payments_attributes=>[:amount, :payment_method_id, :payment_method, {:source_attributes=>[:number, :month, :year, :expiry, :verification_value, :first_name, :last_name, :cc_type, :gateway_customer_profile_id, :gateway_payment_profile_id, :last_digits, :name, :encrypted_data, :existing_card_id,
{
:address_attributes=>[
:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}
]
}
]}], :shipments_attributes=>[:special_instructions, :stock_location_id, :id, :tracking, :selected_shipping_rate_id]}, {:line_items_attributes=>[:id, :variant_id, :quantity]}]
因此,这里有address_属性。奇怪的是它们被{}包裹着。我的理解是,这意味着它在寻找类似这样的参数:order=>{[address_attributes=>{firstname=>…等等。但我不是肯定的。我不确定Solidus为什么要寻找以这种方式传递的参数,我也不确定如何编写我的表单,以便它能像那样传递参数。这是我的表单:
_cart_form.html.erb
然后我的表单工作。所以,我有一个解决办法,但我想了解这里发生了什么。有几种方法可以解决这个问题:
以满足现有订单参数期望的形式传递参数。
修改允许的\u顺序\u属性,以便在传入参数时接受这些参数。
我将允许的_order_属性的内容转储到我喜欢使用的JSON格式工具中,并执行以下操作:
[
: coupon_code,
: email,
: special_instructions,
: use_billing,
{
: bill_address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
],
: ship_address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
],
: payments_attributes=>[
: amount,
: payment_method_id,
: payment_method,
{
: source_attributes=>[
: number,
: month,
: year,
: expiry,
: verification_value,
: first_name,
: last_name,
: cc_type,
: gateway_customer_profile_id,
: gateway_payment_profile_id,
: last_digits,
: name,
: encrypted_data,
: existing_card_id,
{
: address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
]
}
]
}
],
: shipments_attributes=>[
: special_instructions,
: stock_location_id,
: id,
: tracking,
: selected_shipping_rate_id
]
},
{
: line_items_attributes=>[
: id,
: variant_id,
: quantity
]
}
]
在我看来,地址属性的唯一出现似乎是嵌套在支付属性>源属性中。这听起来像是你想要的结构被打破了
对于受保护的参数,最好忽略它们建议的代码,并以正常方式显式构造所需的结构:
def order_params
params.require(:order).permit(:your, :choice, :of, :fields,
address_attributes: [:address, :fields],
other_nested_thing_attributes: [:stuff, :here])
end
您将100%控制您希望从表单中接受的内容。这也将有助于您突出显示表单结构中的错误
HTH-评论或问题,大声说。啊,这很有道理!是的,我只是试图从Solidus劫持尽可能多的现有基础设施。看起来address_属性是设计用于特定方式的。听起来我的解决方法是可行的,尽管也许我应该在其他地方定义这些允许的属性。谢谢s:@WylliamJudd您在哪里为允许的属性放置了order_参数?@Mink-我认为这将在控制器中用于向ActiveRecord对象大量分配值,例如@order.neworder_参数或@order.update_attributesorder_参数。
<%= f.fields_for('address_attributes[][]', @address) do |form| %>
<%= render :partial => 'spree/address/form', :locals => { form: form,
address_type: 'shipping', address: @address } %>
<% end %>
def order_params
params.require(:order).permit(:address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}])
end
[
: coupon_code,
: email,
: special_instructions,
: use_billing,
{
: bill_address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
],
: ship_address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
],
: payments_attributes=>[
: amount,
: payment_method_id,
: payment_method,
{
: source_attributes=>[
: number,
: month,
: year,
: expiry,
: verification_value,
: first_name,
: last_name,
: cc_type,
: gateway_customer_profile_id,
: gateway_payment_profile_id,
: last_digits,
: name,
: encrypted_data,
: existing_card_id,
{
: address_attributes=>[
: id,
: firstname,
: lastname,
: first_name,
: last_name,
: address1,
: address2,
: city,
: country_id,
: state_id,
: zipcode,
: phone,
: state_name,
: country_iso,
: alternative_phone,
: company,
{
: country=>[
: iso,
: name,
: iso3,
: iso_name
],
: state=>[
: name,
: abbr
]
}
]
}
]
}
],
: shipments_attributes=>[
: special_instructions,
: stock_location_id,
: id,
: tracking,
: selected_shipping_rate_id
]
},
{
: line_items_attributes=>[
: id,
: variant_id,
: quantity
]
}
]
def order_params
params.require(:order).permit(:your, :choice, :of, :fields,
address_attributes: [:address, :fields],
other_nested_thing_attributes: [:stuff, :here])
end