0

I'm new to RoR and I'm doing a small application just to get me started.

I have a suppliers page where it lists all the suppliers and inside that I have a link_to the 'new' products page where I can create a new product, I need to send the supplier id through the link_to to the products page and gets stored in a hidden_field_tag value

here's my products page

<%= form_for @product, :url => {:action => "create"} do |f| %>

        <%= f.label :product_name %>
        <%= f.text_field :name %>



        <%= collection_select(:product, :product_category_id, ProductCategory.all, :id, :category,  {prompt: 'Select category'}) %>

        <%= hidden_field_tag(:supplier_id, Supplier.first[:id]) %>

        <%= f.submit "Create a product", class: "btn btn-primary"%>

    <% end %>`

the Supplier.first[:id] is just to try the code

and this is my suppliers page

<% provide(:title, 'All suppliers') %>
<h1>All suppliers</h1>
<ul class="suppliers">
    <% @suppliers.each do |supplier| %>
    <li>
        <%= link_to supplier.name %>
        <%= link_to "Add a product", products_path, :class => "btn btn-primary",:method => "get" %>

        <div>
            <% if supplier.products.any? %>
            <h5>Products (<%= supplier.products.count %>)</h5>
            <ol>
            <% supplier.products.each do |product| %>

                <li><%= product.name %></li>
                <% end %>
            </ol>
            <% end %>
        </div>
    </li>
    <% end %>
</ul>

Any ideas ?

EDIT:

class ProductsController < ApplicationController



    def new
        @product = Product.new

    end

    def create

        @product = Product.new(params[:product])
        if @product.save
            redirect_to "/products"
        else
            render 'new'
        end
    end

EDIT 2: my products model:

class Product < ActiveRecord::Base
  attr_accessible :name, :supplier_id, :product_category_id

  attr_accessor :supplier_id, :product_category_id


  belongs_to :supplier

  belongs_to :product_category

  validates :supplier_id, presence: true
  validates :product_category_id, presence: true

end

and when I run rake routes I get this :

karam@karam-Inspiron-N5010:~/rails-projects/devise$ rake routes
       new_admin_session GET    /admins/sign_in(.:format)      devise/sessions#new
           admin_session POST   /admins/sign_in(.:format)      devise/sessions#create
   destroy_admin_session DELETE /admins/sign_out(.:format)     devise/sessions#destroy
            admin_unlock POST   /admins/unlock(.:format)       devise/unlocks#create
        new_admin_unlock GET    /admins/unlock/new(.:format)   devise/unlocks#new
                         GET    /admins/unlock(.:format)       devise/unlocks#show
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                products POST   /products(.:format)            products#create
             new_product GET    /products/new(.:format)        products#new
                 product GET    /products/:id(.:format)        products#show
           suppliers_new        /suppliers/new(.:format)       suppliers#new
               suppliers        /suppliers(.:format)           suppliers#index
        suppliers_create        /suppliers/create(.:format)    suppliers#create
                                /products(.:format)            products#new

EDIT3 :

My ProductCategory model:

class ProductCategory < ActiveRecord::Base
  attr_accessible :category

  has_many :products


end

this model's controller is empty.

Kmelkon
  • 115
  • 9

1 Answers1

0

Simply do:

<%= link_to "Add a product", products_path(supplier_id: supplier.id), :class => "btn btn-primary" %>

(you do not need :method => "get" since link_to are GET by default)

Also, the following is not correct:

<%= link_to supplier.name %>

Do this instead:

<%= link_to supplier.name, supplier_path(supplier) %>

You should clean your controller as follow:

class ProductsController < ApplicationController
  def new
    @product = Product.new
  end

  def create
    @product = Product.new(params[:product])
    if @product.save
      redirect_to products_path
    else
      render :new
    end
  end
end
Pierre-Louis Gottfrois
  • 17,561
  • 8
  • 47
  • 71
  • @pireer-Louis Gottfrois I did as you said and this is the URL I got http://localhost:3000/products?supplier_id=2 now I want to store that in the hidden_field_tag in the products page but I can't figure it out – Kmelkon Jul 03 '13 at 06:15
  • You can access URL and FORM params using the `params` hash in your views and / or your controllers. `params[:supplier_id]` will retreive the value `2` in your case. You can add that value to your hidden field with `hidden_field_tag :supplier_id, params[:supplier_id]` – Pierre-Louis Gottfrois Jul 03 '13 at 09:59
  • Yesss thank you, it worked. now I have two problems to solve *the create action is not creating the product *I need the URL to be clean as in /products/2... I'll put the products controller in the edit section.. thanks again for your help – Kmelkon Jul 03 '13 at 10:12
  • thanks again, the thing is when I try to create a product both my `supplier_id` and `product_category_id` stay `nil` and the product does not get created, even when I try that from the consol, I've updated my answer, please take a look – Kmelkon Jul 03 '13 at 12:15
  • `attr_accessor :supplier_id, :product_category_id` will create a getter and a setter method for you. Since you have a `belongs_to :supplier` relation set, you cannot have an `attr_accessor :supplier_id` the relations will take care of that for you. Where does the`:product_category_id` comes from ? – Pierre-Louis Gottfrois Jul 03 '13 at 15:07
  • `:product_category_id` comes from a model called `ProductCategory`, so I need to remove my `supplier_id` from the attr_accessor ? I'll provide you with more info in the third edit. – Kmelkon Jul 04 '13 at 05:53
  • yes, remove all attributes in the `attr_accessor`. Read http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby to understand what `attr_accessor` stands for. – Pierre-Louis Gottfrois Jul 04 '13 at 07:12