Unpermitted parameter: nested attributes(deeper)

38 views Asked by At

Error:

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"} }

I wanna create order for online shop, but can't set parameters for address. I had such error with order detail but when I added accepts_nested_attributes_for :order_detail that error disappeared but address didn't

Controller with order_params method:

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

Order model:

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

Order detail model:

class OrderDetail < ApplicationRecord
  belongs_to :order
  belongs_to :address

  accepts_nested_attributes_for :address
end

Address model:

class Address < ApplicationRecord
  belongs_to :user, optional: :true
  has_one :order_detail, dependent: :destroy
end

View:

<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>

I've tried

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]])
0

There are 0 answers