【问题标题】:Rails Subdomain routing with resources not working as expected资源未按预期工作的 Rails 子域路由
【发布时间】:2012-10-04 07:06:55
【问题描述】:

在一个标准的博客应用程序中,我已将此路由添加到我现有的路由中:

match '', to: 'blogs#show', constraints: { subdomain: /.+/ }

这些是/是我已经存在的路线:

resources :blogs do
  member { get :settings }
  member { get :stats }
  member { match ':year/:month/:day/:article_id', to: 'blogs#show_article', :constraints => { year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ } }
end

在我的控制器中,我执行@blog = Blog.find(request.subdomain),为了简单起见,我使用了id。稍后我会使用 blogs slug 或额外的域属性。

只要http://17.lvh.me:3000 将显示博客 17,这工作正常。但我的成员操作未按预期路由。在博客子域上,我希望 http://8.lvh.me:3000/settings,但只有 http://17.lvh.me:3000/blogs/17/settings 有效。

那么我如何告诉我的博客资源成员操作应该在子域下路由而不需要额外的冗余 /blogs/:id?我真的需要手动完成这一切吗? 我觉得我错过了什么。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routes ruby-on-rails-3.2 subdomain


    【解决方案1】:

    试试这个:

    scope '/' do
     with_options :conditions => {:subdomain => /.+/}, :controller => :blogs do |site|
      site.match '', :action => :show
      site.get '/settings', :action => :settings
      site.get '/stats', :action => :stats
      site.match ':year/:month/:day/:article_id', to: 'blogs#show_article', :constraints => { year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ }  
     end
    end
    

    您必须在控制器中的每个操作中都有Blog.find(request.subdomain)。

    附:我知道这是a good theoretical exercise in using subdomains in Rails,但我个人更喜欢clean URLs。

    【讨论】:

    • 感谢您的回答!我不知何故无法让它与我现有的路线一起工作。现在完全没有子域。稍后会接近它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2014-05-02
    • 1970-01-01
    • 2010-11-20
    • 2016-08-19
    • 2015-09-09
    相关资源
    最近更新 更多