1
votes

I have a products index which has a list of categories. I used named scopes in my products model for each category, with each category in the view having a link routing users to a products index filtered accordingly. I have a list of routes for each scope. In the view, I want to replace the route path dynamically with a variable such as VAR_products_path. What's the proper syntax for it?

Products index

- @categories.each do |cat|
- @product = Product.where("category_id = ?", cat.id)
= link_to "#{cat.category_name}", cat.category_name_products_path# architecture_products_path or arts_products_path  

Products controller

def architecture
params[:category]
@categories = Category.all
@products = Product.architecture
render action: :index
end

Routes file

resources :products do
collection do
get :architecture 
get :arts
get :business
...

Product model

scope :architecture, -> { where(category_id: 1) }
scope :arts -> { where(category_id: 2) }
1

1 Answers

1
votes
= link_to cat.category_name, public_send("#{cat.category_name}_products_path")

Although I'd consider changing your routes to take the category name as an argument, for example /products/{category} so you could use it more nicely products_path(cat.category_name).