提问人:Paul Industrious 提问时间:5/31/2023 最后编辑:PipetusPaul Industrious 更新时间:6/1/2023 访问量:34
不允许的参数:嵌套属性(更深)
Unpermitted parameter: nested attributes(deeper)
问:
错误:
Unpermitted parameter: :address. Context: { controller: OrdersController, action: create, request: #<ActionDispatch::Request:0x000000010b1538d8>, params: {"authenticity_token"=>"[FILTERED]", "order"=>{"order_detail_attributes"=>{"first_name"=>"Stack", "last_name"=>"Over", "email"=>"[email protected]"}, "address"=>{"country"=>"Ukraine", "city"=>"Lviv", "street"=>"brooklyn", "comment"=>"hi there!"}}, "commit"=>"Create", "controller"=>"orders", "action"=>"create"} }
我想为网上商店创建订单,但无法设置地址参数。我在订单详细信息方面遇到了这样的错误,但是当我添加accepts_nested_attributes_for:order_detail该错误消失了,但地址没有
采用order_params方法的控制器:
class OrdersController < ApplicationController
def new
@order = Order.new
@order.build_order_detail
@order.order_detail.build_address
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to @order, notice: "Order was successfully created."
else
render :new
end
end
def index
@user_orders = current_user.orders
end
def show
@order = Order.find(params[:id])
end
private
def order_params
params.require(:order).permit(:status, :ordered_at, :user_id,
order_detail_attributes: [:first_name, :last_name, :email, address_attributes: [:country, :city, :street, :comment]])
end
end
订货型号:
class Order < ApplicationRecord
belongs_to :user, optional: :true
has_one :order_detail, dependent: :destroy
has_one :address, through: :order_detail
has_many :product_orders, dependent: :destroy
has_many :products, through: :product_orders
accepts_nested_attributes_for :order_detail
end
订单明细型号:
class OrderDetail < ApplicationRecord
belongs_to :order
belongs_to :address
accepts_nested_attributes_for :address
end
地址模型:
class Address < ApplicationRecord
belongs_to :user, optional: :true
has_one :order_detail, dependent: :destroy
end
视图:
<div class="row justify-content-center ">
<div class="col-md-7 col-lg-5">
<div class="wrap">
<div class="login-wrap p-4 p-md-1">
<%= form_with(model: @order, url: orders_path, local: true) do |form| %>
<h2 class="tw tc">
<span class="rounded bg-text">
Order Details
</span>
</h2>
<%= form.fields_for :order_detail do |order_detail_form| %>
<div class="form-floating">
<%= order_detail_form.text_field :first_name, class: "form-control" %>
<%= order_detail_form.label :first_name, "First name", class: "form-control-placeholder" %>
</div>
<div class="form-floating">
<%= order_detail_form.text_field :last_name, class: "form-control" %>
<%= order_detail_form.label :last_name, "Last name", class: "form-control-placeholder" %>
</div>
<div class="form-floating">
<%= order_detail_form.text_field :email, class: "form-control" %>
<%= order_detail_form.label :email, "Email", class: "form-control-placeholder" %>
</div>
<% end %>
<%= form.fields_for :address do |address_form| %>
<div class="form-floating">
<%= address_form.text_field :country, class: "form-control" %>
<%= address_form.label :country, "Country", class: "form-control-placeholder" %>
</div>
<div class="form-floating">
<%= address_form.text_field :city, class: "form-control" %>
<%= address_form.label :city, "City", class: "form-control-placeholder" %>
</div>
<div class="form-floating">
<%= address_form.text_field :street, class: "form-control" %>
<%= address_form.label :street, "Street", class: "form-control-placeholder" %>
</div>
<div class="form-floating">
<%= address_form.text_area :comment, class: "form-control" %>
<%= address_form.label :comment, "Comment", class: "form-control-placeholder" %>
</div>
<% end %>
<div class="form-floating tc">
<%= form.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
我试过了
accepts_nested_attributes_for :order_detail, :address
params.require(:order).permit(:status,
order_detail_attributes: [:first_name, :last_name, :email,
address_attributes: [:country, :city, :street, :comment]])
答: 暂无答案
评论