How to get outlet id which didn’t have at product migration table and show it at add page

Viewed 16

I’m new in Ruby on Rails, how can I get the outlet id which is not at the product migration table , because category id has at product migration table, I can get it through product but how outlet id? I want to get it and put at the add item there and the result will store at OutletProduct table.I have do some but seem is not correct. 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
0 Answers
Related