将根 url 重定向到 Rails 应用程序中的其他位置

发布于 2024-08-17 20:35:50 字数 2731 浏览 2 评论 0 原文

我有这样的路线:

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 上的示例中找到了它,但我也发现了添加此类功能的插件。

感谢您的帮助!

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.

Earlier when I used map.root from above example it just stayed at www.example.com and rendered correct view (but it was without :locale and it worked good), later I added :locale to 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.

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:

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

EDIT:
My rake routes looks like this:

         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).

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.

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦明 2024-08-24 20:35:50

在 Rails 3 中,您可以编写:

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

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

In Rails 3 you can write:

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

梦年海沫深 2024-08-24 20:35:50

根重定向选项似乎不是记录得很好
在这里(@derek,请参阅最后一个示例):

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

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

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

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

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

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

使用条件块重定向

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

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

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)

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
}
我是男神闪亮亮 2024-08-24 20:35:50

在 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.rbservers_controller.rb。在该文件中,您将看到一些如下所示的代码:

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

在有 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] to 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 NOT application_controller.rb or servers_controller.rb. In that file, you'll see some code that looks like this:

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

风轻花落早 2024-08-24 20:35:50

您需要将控制器设置为欢迎或其他,然后当控制器被击中时,它将重定向到您想要的路线。也许 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文