Ruby on rails 自定义约束是否可以路由到多个控制器动作?

Ruby on rails 自定义约束是否可以路由到多个控制器动作?,ruby-on-rails,Ruby On Rails,目前,我正在使用链接到单个管线的自定义约束,如: get '/hello', to: 'account#index', constraints: AccountConstraint.new 基本上,我的自定义约束是查找request.host,如果在数据库中找到它,则匹配?方法将返回true,然后将调用account#index操作 我想做的是,如果约束匹配,那么它将根据路径执行不同的操作 因此,我的约束条件是: class AccountConstraint def m

目前,我正在使用链接到单个管线的自定义约束,如:

get '/hello', to: 'account#index', 
        constraints: AccountConstraint.new
基本上,我的自定义约束是查找request.host,如果在数据库中找到它,则匹配?方法将返回true,然后将调用account#index操作

我想做的是,如果约束匹配,那么它将根据路径执行不同的操作

因此,我的约束条件是:

class AccountConstraint
  def matches?(request)
    # lookup the database, return true if record found
  end
end
然后我希望我的route.rb文件执行如下操作(下面的伪代码):

if AccountConstraint matches
  get '/', to: "account#index"
  get '/hello', to: "account#hello"
end

这样的事情可能吗?如果可能,怎么可能?

我不确定我是否理解这个问题,但听起来你想要的是一个
范围

scope constraints: AccountConstraint.new do
  get '/', to: "account#index"
  get '/hello', to: "account#hello"
end

只有当
AccountConstraint
匹配时,才能访问作用域内的路由。

我将在控制器内而不是在路由文件中写入此类逻辑。如果请求匹配,您可以在控制器中进行检查,甚至在视图中也可以进行检查。您应该使用控制器在控制器中检查来自请求的引用器/主机,而不是比相应的路由和重定向要好得多。