Ruby on rails 如何处理路由错误Rails 4

Ruby on rails 如何处理路由错误Rails 4,ruby-on-rails,redirect,routing,Ruby On Rails,Redirect,Routing,我想知道什么是处理特定控制器路由错误的最佳实践和方法 就我而言,我有两个资源 resources :user resources :special_user 我希望抛出路由错误的所有内容都重定向回该资源索引,例如: 请求mydomain.com/users/blahblah,将重定向到mydomain.com/users 特殊用户mydomain.com/special_users/blahblah将重定向回mydomain.com/special_users 最好的方法是什么?您可以: r

我想知道什么是处理特定控制器路由错误的最佳实践和方法

就我而言,我有两个资源

resources :user
resources :special_user 
我希望抛出路由错误的所有内容都重定向回该资源索引,例如:

请求
mydomain.com/users/blahblah
,将重定向到
mydomain.com/users

特殊用户
mydomain.com/special_users/blahblah
将重定向回
mydomain.com/special_users

最好的方法是什么?

您可以:

resources :users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/users')
end
resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/special_users')
end

另一种方法是:

resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', action: :catch_all
end
在您的特殊用户控制器中,您定义了以下操作:

def catch_all
  # maybe set some flash message
  redirect_to special_users_path
end

感谢@JiříPospíšil的评论,我意识到你可能还有一件事要检查

在显示操作中(例如,针对用户):

(我假设您忘记了将资源多元化)

您可以:

resources :users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/users')
end
resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/special_users')
end

另一种方法是:

resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', action: :catch_all
end
在您的特殊用户控制器中,您定义了以下操作:

def catch_all
  # maybe set some flash message
  redirect_to special_users_path
end

感谢@JiříPospíšil的评论,我意识到你可能还有一件事要检查

在显示操作中(例如,针对用户):

(我假设您忘记了将资源多元化)

您可以:

resources :users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/users')
end
resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/special_users')
end

另一种方法是:

resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', action: :catch_all
end
在您的特殊用户控制器中,您定义了以下操作:

def catch_all
  # maybe set some flash message
  redirect_to special_users_path
end

感谢@JiříPospíšil的评论,我意识到你可能还有一件事要检查

在显示操作中(例如,针对用户):

(我假设您忘记了将资源多元化)

您可以:

resources :users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/users')
end
resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', to: redirect('/special_users')
end

另一种方法是:

resources :special_users do
  # if you need to add new routes, add them before the catch all
  get  '*a', action: :catch_all
end
在您的特殊用户控制器中,您定义了以下操作:

def catch_all
  # maybe set some flash message
  redirect_to special_users_path
end

感谢@JiříPospíšil的评论,我意识到你可能还有一件事要检查

在显示操作中(例如,针对用户):


(我假设您忘记了将资源多元化)

我认为您需要做的不是处理路由错误,而是考虑路由文件中的“随机”情况。在意外情况真正导致错误之前,最好先预测并处理它。我认为您需要做的不是处理路由错误,而是考虑路由文件中的“随机”情况。在意外情况真正导致错误之前,最好先预测并处理它。我认为您需要做的不是处理路由错误,而是考虑路由文件中的“随机”情况。在意外情况真正导致错误之前,最好先预测并处理它。我认为您需要做的不是处理路由错误,而是考虑路由文件中的“随机”情况。总是最好在意外事件实际导致错误之前预测和处理它。这真的有助于OP请求mydomain.com/users/blahblah重定向到mydomain.com/users?从路由器的角度来看,对
users/blahblah
的请求是完全有效的,它是对
userscoontroller#show
id=blahblah
的请求。路径
get'*a',to:redirect('/users')
仅在存在约束冲突(例如,请求
/users/bla/bla
)时才会匹配。我遗漏了什么?这真的有助于OP向mydomain.com/users/blahblah发出
请求,并重定向到mydomain.com/users
?从路由器的角度来看,对
users/blahblah
的请求是完全有效的,它是对
userscoontroller#show
id=blahblah
的请求。路径
get'*a',to:redirect('/users')
仅在存在约束冲突(例如,请求
/users/bla/bla
)时才会匹配。我遗漏了什么?这真的有助于OP向mydomain.com/users/blahblah发出
请求,并重定向到mydomain.com/users
?从路由器的角度来看,对
users/blahblah
的请求是完全有效的,它是对
userscoontroller#show
id=blahblah
的请求。路径
get'*a',to:redirect('/users')
仅在存在约束冲突(例如,请求
/users/bla/bla
)时才会匹配。我遗漏了什么?这真的有助于OP向mydomain.com/users/blahblah发出
请求,并重定向到mydomain.com/users
?从路由器的角度来看,对
users/blahblah
的请求是完全有效的,它是对
userscoontroller#show
id=blahblah
的请求。路径
get'*a',to:redirect('/users')
仅在存在约束冲突(例如,请求
/users/bla/bla
)时才会匹配。我错过了什么?