使用变量更改路径路径名称

时间:2016-06-04 04:01:28

标签: ruby-on-rails-4 routes

我有一个产品索引,其中包含一个类别列表。我在每个类别的产品模型中使用了命名范围,视图中的每个类别都有一个链接路由用户到相应过滤的产品索引。我有一个每个范围的路由列表。在视图中,我想用一个变量(如VAR_products_path)动态替换路径路径。它的正确语法是什么?

产品索引

- @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  

产品控制器

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

路由文件

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

产品型号

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

1 个答案:

答案 0 :(得分:1)

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

虽然我考虑更改您的路线以将类别名称作为参数,例如/products/{category},这样您就可以更好地使用它products_path(cat.category_name)

相关问题