Stack Exchange
NoMethodError in SuppliersController#create undefined method `outlet_suppliers'
now I'm trying store data (supplier_id and outlet_id) to outlet_suppliers from create supplier form, but it shown some error which is undefined method `outlet_suppliers'. Below is my code:
suppliers controller
def new
 @supplier = Supplier.new
 @outlet = Outlet.all
 @supplier.company_id = params[:company_id]
end

def create
 @supplier = Supplier.new(supplier_params)
 @company_id = Company.find(params[:supplier][:company_id])
 @outlet_id = Outlet.all
 if @supplier.save
 @outlet_supplier = Outlet.find(params[:supplier][:outlet_id]).outlet_suppliers.create(supplier: @supplier)
  redirect_to @supplier
else
  render 'new'
 end

end
new.html.erb
<% provide(:title, "Create Suppliers") %>
<h1>Create Supplier</h1>

<div class="row">
 <div class="col-md-6 col-md-offset-3">
  <%= form_with(model: @supplier, local: true) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

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

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

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

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

  <%= f.hidden_field :company_id , value: 2%>


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

outlet_suppliers migration
create_table "outlet_suppliers", force: :cascade do |t|
 t.integer "outlet_id"
 t.integer "supplier_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

Error show in website

Outlet indeed does not seem to have this method. Did you forget to add the relationship to the model?

ohh yea, i forgot add it to outlet, thanks

issue is with your model file as pointed out by @Sergio in a comment.
#app/models/outlet_supplier.rb 
class OutletSupplier < ApplicationRecord
  belongs_to :outlet
  belongs_to :supplier 
  #...
end

#app/models/outlet.rb 
class Outlet < ApplicationRecord
  has_many :outlet_suppliers
  #...
end

I hope this will help you.

yes, i forgot to add it to outlet, thanks~
