Home > Blockchain >  Unpermitted parameters: :supplier, :outlet
Unpermitted parameters: :supplier, :outlet

Time:09-28

now i'm doing a grocery website. I trying to build an order form that can choose select supplier and outlet,so admin can create order by choosing which supplier and outlet, but now it come out error say unpermitted parameters.

Orders Controller

 class OrdersController < ApplicationController
  def supplier
   @supplier = Supplier.find(params[:id])
  end

  def outlet
   @outlet = Outlet.find(params[:id])
  end

  def index
   @orders = Order.all
  end

  def show
   @order = Order.find(params[:id])
  end

  def new
   @order = Order.new
   @supplier = Supplier.all
   @outlet = Outlet.all
  end

  def create
   @order = Order.new(order_params)
   @supplier_id = Supplier.all
   @outlet_id = Outlet.all
   if @order.save
    flash[:success] = "Succesful create!"
   redirect_to @order
 else
   render 'new'
  end
end

private

def order_params
  params.require(:order).permit(:supplier_id,:grand_total, :order_date,
                                :delivery_date, :delivery_address, :outlet_id)
 end
end

new.html.erb

<% provide(:title, "Create Orders") %>
<h1>Create Order</h1>

<div >
 <div >
  <%= form_with(model: @order, local: true) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :supplier %>
  <%= f.select(:supplier, Supplier.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>

  <%= f.label :grand_total %>
  <%= f.number_field :grand_total, class: 'form-control' %>

  <%= f.label :order_date %>
  <%= f.date_field :order_date, class: 'form-control' %>

  <%= f.label :delivery_date %>
  <%= f.date_field :delivery_date, class: 'form-control' %>

  <%= f.label :delivery_address %>
  <%= f.text_field :delivery_address, class: 'form-control' %>

  <%= f.label :outlet %>
  <%= f.select(:outlet, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>


   <%= f.submit "Create order", class: "btn btn-primary" %>
  <% end %>
 </div>
</div>

Order migration table in schema

create_table "orders", force: :cascade do |t|
t.integer "supplier_id"
t.integer "grand_total"
t.date "order_date"
t.date "delivery_date"
t.string "delivery_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "outlet_id"

end

parameter in console

error in website

CodePudding user response:

The reason you're getting

Unpermitted parameters: :supplier, :outlet

is because you're not allowing :supplier and :outlet params but :supplier_id and :outlet_id in order_params

The Fix

Change this in your view new.html.erb

<%= f.label :supplier_id %>
<%= f.select(:supplier_id, Supplier.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>

<%= f.label :outlet_id %>
<%= f.select(:outlet_id, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>

This should fix the issue by allowing the :supplier_id and :outlet_id params and assigning the object with proper values

  • Related