1

我有路线规则:

match '*path' => redirect("/some_scope/%{path}")

但只有当当前路径不是从/some_scope/

4

2 回答 2

1

http://guides.rubyonrails.org/routing.html#dynamic-segments

如何在 Rails 路由中使用正则表达式进行重定向?

这两个应该能够帮助您解决问题。如果你给我们真正的代码会很好,所以我可以准确地确定你想要做什么,因为在这种情况下你似乎甚至不需要正则表达式。

如果您正在尝试做我认为您正在尝试做的事情......那就是如果路径尚未包含该范围,则将范围应用于路径,您最好before_filtersApplicationController. 那是:

before_filter :check_locale

protected

def check_locale
  redirect_to "/some_scope#{request.path_info}" if not request.path_info =~ /^\/some_scope\//
end
于 2013-05-25T16:42:15.147 回答
0

这是我的解决方案:

DEFAULT_PATH_SCOPE = "/my_scope"

default_scope_constraint = ->(request) do
  !request.path.starts_with?(DEFAULT_PATH_SCOPE)
end

constraints(default_scope_constraint) do
  match '*path' => redirect("#{DEFAULT_PATH_SCOPE}/%{path}")
end
于 2013-05-27T13:42:28.897 回答