Ruby-on-rails 将根 URL 重定向到 Rails 应用程序中的其他位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2066504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:09:08  来源:igfitidea点击:

Redirect root url to somewhere else in Rails application

ruby-on-railsredirectroutesroot

提问by klew

I have routes like this:

我有这样的路线:

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'

What I would like to get is that after typing: www.example.com it would redirect me to www.example.com/en/prepayments.

我想得到的是,在输入:www.example.com 后,它会将我重定向到 www.example.com/en/prepayments。

Earlier when I used map.rootfrom above example it just stayed at www.example.com and rendered correct view (but it was without :localeand it worked good), later I added :localeto my routes and from this time my view (that uses some form) doesn't work properly. I get error that it can't find corresponding route for form - which is right, because I didn't pass any :locale.

早些时候,当我map.root从上面的例子中使用它时,它只是停留在 www.example.com 并呈现正确的视图(但它没有:locale并且效果很好),后来我添加:locale到我的路线中,从这个时候我的视图(使用某种形式)没有不能正常工作。我收到错误,它找不到表单的相应路径 - 这是正确的,因为我没有通过任何:locale.

So, how to redirect root to another page? It will probably need to generate correct path and pass it through http 302. Or/And how to make something like:

那么,如何将 root 重定向到另一个页面?它可能需要生成正确的路径并通过 http 302 传递它。或者/以及如何制作类似的东西:

map.root :controller => 'prepayments/login', :my_locale => 'en'

EDIT: My rake routes looks like this:

编辑:我的 rake 路线如下所示:

         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"}

EDIT:

编辑:

I tried doing it in the way Garrett proposed and it worked. I changed routes:

我试着按照加勒特提议的方式去做,并且奏效了。我改变了路线:

map.redirect '/', :controller => 'prepayments/login', :action => 'welcome'

and added welcome method in controller:

并在控制器中添加了欢迎方法:

def welcome
  redirect_to prepayments_root_path(:locale => 'en')
end

And it works as I wanted (so it changes url in my browser).

它可以按我的意愿工作(因此它会更改浏览器中的 url)。

The other way is to change routes like this:

另一种方法是改变这样的路线:

map.root :controller => 'prepayments/login', :locale => 'en'

It also works, but it isn't redirecting (it doesn't change url in browser). I'm not sure if there is such option as map.redirect. I found it in examples on www but I also found plugin that add such functionality.

它也可以工作,但它不会重定向(它不会更改浏览器中的 url)。我不确定是否有这样的选项map.redirect。我在 www 上的示例中找到了它,但我也找到了添加此类功能的插件。

Thanks for help!

感谢帮助!

采纳答案by Garrett

You will need to set the controller to a Welcomeor what not, then when thatcontroller 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.

您需要将控制器设置为 aWelcome或什么不是,然后当控制器被击中时,它将重定向到您想要的路线。也许 Rails 3 路由对这样的东西会更好,但现在,你需要有一个主根控制器。

回答by Shaun McDonald

In Rails 3 you can write:

在 Rails 3 中,你可以这样写:

root :to => redirect('/prepayments')

The following page has a good introduction to doing these redirects in Rails 3: http://www.railsdispatch.com/posts/rails-routing

以下页面很好地介绍了在 Rails 3 中进行这些重定向:http: //www.railsdispatch.com/posts/rails-routing

回答by glasz

redirect optionsdon't seem to be documentedtoo well.
here you go (@derek, see last example):

重定向选项似乎没有很好地记录
给你(@derek,见最后一个例子):

redirect to a subdomain on the current request's domain

重定向到当前请求域上的子域

root to: redirect(subdomain: 'foo', path: '/bar') # => foo.example.com/bar

redirect with substituted params from the matched route

使用匹配路由中的替换参数重定向

get 'stores/:name(*all)', to: redirect(subdomain: 'stores', path: '/%{name}%{all}')

redirect with status code (eg. 302 instead of default 301)

使用状态代码重定向(例如 302 而不是默认的 301)

redirect(path: '/foo', status: 302)

redirect with a conditional block

使用条件块重定向

redirect(status: 302) { |params, request|
  case request.host
  when 'localhost'
    '/foo'
  when /example.com$/
    '/bar'
  else
    '/baz'
  end
}

回答by Jeremy

In Rails 4 (4.2.0 in my case), I added this: match "*path" => "main#index", :via => [:get, :post]to app/config/routes.rb.

在 Rails 4(在我的例子中是 4.2.0)中,我添加了这个:match "*path" => "main#index", :via => [:get, :post]app/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 called main_controller.rb, but it will be something_controller.rb, probably NOTapplication_controller.rbor servers_controller.rb. In that file, you'll see some code that looks like this:

找出你应该用什么来代替main. 查看这个文件:app/controllers/main_controller.rb. 同样,您的可能不会被称为main_controller.rb,但它会被称为something_controller.rb,可能不是application_controller.rbservers_controller.rb。在该文件中,您将看到一些如下所示的代码:

class MainController < ApplicationController
  def index
    render :layout => "angular"
  end
end

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 mainin match "*path" => "main#index", :via => [:get, :post]with whatever the prefix of the controller is.

哪里有MainController,您应该能够通过 rails 在浏览器中提供的错误消息来判断从这里开始做什么。只需将mainin替换match "*path" => "main#index", :via => [:get, :post]为控制器的前缀即可。

Hope this makes sense. I am a ruby beginner as well

希望这是有道理的。我也是 ruby​​ 初学者