Ruby on rails Rails:重定向到当前路径但不同的子域

Ruby on rails Rails:重定向到当前路径但不同的子域,ruby-on-rails,routing,Ruby On Rails,Routing,我认为这应该是相当容易,但我不熟悉它是如何做到的 如何编写before筛选器来检查当前请求是否针对特定子域,如果是,则将其重定向到同一url但位于不同子域上 Ie:blog.myapp.com/posts/1可以,但是blog.myapp.com/products/123重定向到www.myapp.com/products/123 我在想类似于 class ApplicationController < ActionController::Base before_filter :ens

我认为这应该是相当容易,但我不熟悉它是如何做到的

如何编写before筛选器来检查当前请求是否针对特定子域,如果是,则将其重定向到同一url但位于不同子域上

Ie:
blog.myapp.com/posts/1
可以,但是
blog.myapp.com/products/123
重定向到
www.myapp.com/products/123

我在想类似于

class ApplicationController < ActionController::Base
  before_filter :ensure_domain

  protected
  def ensure_domain
    if request.subdomain == 'blog' && controller_name != 'posts'
      redirect_to # the same url but at the 'www' subdomain...
    end
  end
end
class ApplicationController

从子域重定向到子域的简单选择如下

redirect_to subdomain: 'www', :controller => 'www', :action => "index"

这将在域名上调用,例如foo.apple.com将转到www.apple.com

一个更简单的选项,无需执行字符串子项或指定要使用的控制器或操作

redirect_to url_for(subdomain: 'www', only_path: false)

一个简单的解决方案是

redirect_to dummy_rul(subdomain: 'subdomain')
例如:

redirect_to projects_url(subdomain: 'edv') it will redirect on below url

"http://edv.maindomain.com/projects"

“edv”是我的子域

啊,我不知道
.sub
方法存在!谢谢啊!子方法只是简单的旧ruby子方法。。。好的,在进一步的实验之后,这是可行的,但我必须使用更多的正则表达式来处理它。不过,
request.url
对象给了我足够的时间来计算其余的内容,谢谢!