首页 > 解决方案 > Rails - 如果用约束包装,redirect_to 找不到路由

问题描述

我有这样的路线被约束包裹。

constraints DomainConstraint.new('admin.example.com') do
  get '/admin/:page', to: 'admin#browse', as: :admin_index
end

constraints DomainConstraint.new('client.example.com') do
  get '/:page', to: 'client#index', as: :client_index
end

现在,当我想通过以下方式将请求从admin.example.com控制器重定向到client控制器时:

def some_page
  redirect_to action: :client_index, 
end

它说:

No route matches

我相信这是因为我用约束包裹了路线。我如何将管理员重定向到客户端使用constraints

标签: ruby-on-railsruby

解决方案


action如果您未指定控制器名称,则您的代码无法正常工作的原因是params 在同一控制器上采用了操作名称。

更改您的代码

def some_page
   redirect_to action: :client_index, 
end

def some_page
   redirect_to controller: 'client', action: :index 
end

更多信息在redirect_to 这里


推荐阅读