2

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

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?我真的需要手动完成这一切吗?我觉得我错过了什么。

4

1 回答 1

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)在控制器中的每个操作中都有。

PS 我知道这是在 Rails 中使用子域的一个很好的理论练习,但我个人更喜欢干净的 URL

于 2012-10-13T23:23:23.143 回答