【问题标题】:Rails 4 static subdomainRails 4 静态子域
【发布时间】:2014-08-17 21:40:46
【问题描述】:

我无法为子域设置根路径。

这是我的routes.rb 部分:

constraints subdomain: 'blog' do
  scope :module => "blog", :as => "blog" do
    resources :posts
  end
end

root 'statics#index'

当我访问blog.example.com 时,我得到了static#index 操作响应,并在访问blog.example.com/posts 时得到posts#index

我想为 blog.example.com 设置指向 posts#index 的根路径。

对此没有影响:

match '/' => 'posts#index', :constraints => { :subdomain => 'blog' }, via: [:get]

【问题讨论】:

  • 您关心您的 routes.rb 文件中的顺序吗? Rails 将采用第一个匹配的路线。所以总是在受限路线之后放置更多一般路线
  • 是的,例如,'main' root 是 routes.rb 中的最后一条记录

标签: ruby-on-rails ruby-on-rails-4 routes subdomain


【解决方案1】:

我相信你应该仍然可以在constraints 块内调用root

constraints subdomain: 'blog' do
  root :to => 'posts#index' # Perhaps this needs to be `blog/posts#index`?

  scope :module => "blog", :as => "blog" do
    resources :posts
  end
end

root 'statics#index'

【讨论】:

  • 它根本不起作用:当我运行 rails server 时出现错误:Uncaught exception: Invalid route name, already in use: 'root' You may have defined two routes with the same name using the ":as" option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with "resources" as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 当我使用 root :to => 'posts#index', :as => "blog" 时,我可以启动 rails-server,但是 blog.example .com 指向 statics#index
【解决方案2】:

This 为我工作,每个子域都有单独的命名空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2014-03-15
    • 1970-01-01
    相关资源
    最近更新 更多