构建有效路线

时间:2019-06-13 13:52:20

标签: ruby-on-rails rails-routing

我的Rails.application.routes内部有以下路由

get '/c/*name', :to => 'categories#show', :as => :filter_categories
get '/c/*name/*token', :to => 'categories#nest_products', :as => :nest_categories

和该控制器

class CategoriesController < ApplicationController
  def show
    params[:name]
    @products, @filters = ProductFilterService.new(params[:name]).filter_by_taxon
    @selected_taxons = ProductFilterService.new(params[:name]).selected_taxons
  end

  def nest_products
    puts params[:token]
    render :show
  end
end

像这个示例localhost:3000 / adidas / fH72VLNlAma2JWc一样,我想执行方法nest_products,但总是转到方法show。

1 个答案:

答案 0 :(得分:2)

只需像这样更改路由的声明顺序,所以如果您有MiniCssExtractPlugin参数,则它首先匹配nest_categories

token

由于get '/c/*name/*token', :to => 'categories#nest_products', :as => :nest_categories get '/c/*name', :to => 'categories#show', :as => :filter_categories 匹配两条路由,因此您需要先声明最具体的一条。