ActiveRecord::RecordNotFound in ProductsController#add ,Couldn't find Product without an ID

Viewed 61

now i doing the product and outlet, at product info page have an option to add outlet, then admin can choose which outlet to add to, and the respective price.But it keep show this error,no idea why it showing this, can anyone help me? thanks

product controller

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

def add
@product = Product.find(params[:id])
@outlet = Outlet.find(params[:outlet_id])
end

def update
@product = Product.find(params[:id])
@outlet = Outlet.find(params[:outlet][:name])
if @product.update(product_params)
  flash[:success] = "Product updated"
  redirect_to @product
else
  render 'add'
 end
end

show.html.erb

<% provide(:title, @product.name)%>

<%= render @product %>
 <div class="row">
  <aside class="col-md-4">
   <section class="stats">
    <%= render 'shared/stats' %>
   </section>
   <div>
   <%= link_to "Add to Outlet", add_path %> |
   <%= link_to "Back to products", products_path %>
  </div>
</aside>
</div>

add.html.erb

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

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

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

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

  <%= f.label :outlet %>
  <%= f.select :outlet, options_for_select(@outlets), :include_blank => true %>

  <%= f.hidden_field :category_id, value: 1 %>

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

Product migration table

class CreateProducts < ActiveRecord::Migration[7.0]
  def change
   create_table :products do |t|
   t.string :name
   t.integer :quantity
   t.integer :price
   t.integer :category_id

   t.timestamps
  end
 end
end

Error show in website

error show in console

after i click add outlet (in console)

the page that click the add outlet

0 Answers
Related