Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.Please help me, thanks.

Also i get this error messages when i click to the add page:

Started GET "/add" for 127.0.0.1 at 2022-09-25 17:09:11 +0800
Processing by ProductsController#add as HTML
Completed 404 Not Found in 1ms (Allocations: 485)



ActiveRecord::RecordNotFound (Couldn't find Product without an ID):

app/controllers/products_controller.rb:37:in `add'

What I have tried:

inside the code is what i had try but seem like cannot work.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900