nilkash
nilkash

Reputation: 7536

passing id to controller through link_to in railsner

Hi I am beginner to ruby on rails. I have one category table and one product table. I want to display all category and onclick of category display all product related to that category. I am able to display all categories but I dont know how to pass category id to link of that category. I tried this in following way but its not working

 in routes.rb
 get "products", :to => "products#show"

and in view I am creating link to my category in following way

<%= link_to "category.name", products_path %>

its not giving any error but I don't know how to pass id.

I don't know how to do this? Is this the proper way to do this? Need help. Thank you.

Upvotes: 2

Views: 3111

Answers (1)

Miotsu
Miotsu

Reputation: 1776

<%= link_to "category.name", products_path(:product_id => product.id) %>

Then in your method you can access the value through:

params[:product_id]

Upvotes: 4

Related Questions