将根 url 重定向到 Rails 应用程序中的其他位置
我有这样的路线:
map.namespace 'prepayments', :path_prefix => '/:locale/prepayments' do |prepayment|
prepayment.root :controller => 'login', :namespace => 'prepayments'
...
end
map.redirect '/', :controller => 'prepayments/login' # this is not working
# I tried also
map.root :controller => 'prepayments/login'
我想要得到的是,输入 www.example.com 后,它会将我重定向到 www.example.com/en/prepaids。
早些时候,当我使用上面示例中的 map.root
时,它只是停留在 www.example.com 并呈现正确的视图(但它没有 :locale
并且效果很好),后来我将 :locale
添加到我的路线中,从这时起我的视图(使用某种形式)无法正常工作。我收到错误消息,它找不到表单的相应路径 - 这是正确的,因为我没有传递任何 :locale
。
那么,如何将root重定向到另一个页面呢?它可能需要生成正确的路径并通过 http 302 传递它。 或者/以及如何制作类似的东西:
map.root :controller => 'prepayments/login', :my_locale => 'en'
编辑: 我的耙子路线如下所示:
prepayments_root /:locale/prepayments {:controller=>"prepayments/login", :action=>"index"}
prepayments_create /:locale/prepayments/send_email {:method=>:post, :controller=>"prepayments/login", :action=>"send_email"}
prepayments_home /:locale/prepayments/home {:controller=>"prepayments/prepayments", :action=>"home"}
prepayments_save /:locale/prepayments/save {:controller=>"prepayments/prepayments", :action=>"save"}
prepayments_agree /:locale/prepayments/agree {:controller=>"prepayments/prepayments", :action=>"agree"}
prepayments_disagree /:locale/prepayments/disagree {:controller=>"prepayments/login", :action=>"logout"}
prepayments_payment /:locale/prepayments/payment {:controller=>"prepayments/prepayments", :action=>"payment"}
prepayments_payment_email /:locale/prepayments/payment_email {:controller=>"prepayments/prepayments", :action=>"payment_email"}
/:locale/prepayments/:uid {:controller=>"prepayments/login", :action=>"verify"}
redirect / {:controller=>"prepayments/login", :action=>"index"}
编辑:
我尝试按照加勒特提出的方式进行操作,并且它有效。我更改了routes:
map.redirect '/', :controller => 'prepayments/login', :action => 'welcome'
并在控制器中添加了welcome方法:
def welcome
redirect_to prepayments_root_path(:locale => 'en')
end
它按我想要的方式工作(因此它更改了浏览器中的url)。
另一种方法是像这样更改路由:
map.root :controller => 'prepayments/login', :locale => 'en'
它也可以工作,但它不是重定向(它不会更改浏览器中的 url)。我不确定是否有诸如 map.redirect
这样的选项。我在 www 上的示例中找到了它,但我也发现了添加此类功能的插件。
感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Rails 3 中,您可以编写:
以下页面很好地介绍了如何在 Rails 3 中执行这些重定向: http://www.railsdispatch.com railsdispatch.com/posts/rails-routing
In Rails 3 you can write:
The following page has a good introduction to doing these redirects in Rails 3: http://www.railsdispatch.com/posts/rails-routing
根重定向选项似乎不是记录得很好。
在这里(@derek,请参阅最后一个示例):
重定向到当前请求域上的子域
使用匹配路由中的替换参数进行重定向
使用状态代码进行重定向(例如,302 而不是默认的 301)
使用条件块重定向
root redirect options don't seem to be documented too well.
here you go (@derek, see last example):
redirect to a subdomain on the current request's domain
redirect with substituted params from the matched route
redirect with status code (eg. 302 instead of default 301)
redirect with a conditional block
在 Rails 4(我的例子是 4.2.0)中,我添加了以下内容:
match "*path" => “main#index”,:via => [:get, :post]
到app/config/routes.rb
。找出你应该用什么来代替
main
。查看此文件:app/controllers/main_controller.rb
。同样,您的名称可能不是main_controller.rb
,而是something_controller.rb
,可能不是application_controller.rb 或
servers_controller.rb
。在该文件中,您将看到一些如下所示的代码:在有 MainController 的地方,您应该能够通过 Rails 在浏览器中提供的错误消息来判断从这里应该做什么。只需替换
match "*path" => 中的 main 即可“main#index”,:via => [:get, :post]
与控制器的前缀无关。希望这是有道理的。我也是红宝石初学者
In Rails 4 (4.2.0 in my case), I added this:
match "*path" => "main#index", :via => [:get, :post]
toapp/config/routes.rb
.To find out what you are supposed to put in place of
main
. Look in this file:app/controllers/main_controller.rb
. Again, yours may not be calledmain_controller.rb
, but it will besomething_controller.rb
, probably NOTapplication_controller.rb
orservers_controller.rb
. In that file, you'll see some code that looks like this:Where there is MainController, you should be able to tell by the error message that rails provides in your browser what to do from here. Just replace main in
match "*path" => "main#index", :via => [:get, :post]
with whatever the prefix of the controller is.Hope this makes sense. I am a ruby beginner as well
您需要将控制器设置为
欢迎
或其他,然后当该控制器被击中时,它将重定向到您想要的路线。也许 Rails 3 路由对于这样的事情会更好,但现在,您需要有一个主根控制器。You will need to set the controller to a
Welcome
or what not, then when that controller is hit, it will redirect to the route you want. Maybe Rails 3 routing will be better for stuff like this, but for right now, you will need to have a main root controller.