2017-10-16 74 views
0

在我的config/routes.rb中,我有:获取部分路径

resources :landings do 
    collection do 
    get 'about' 
    end 
end 

这给我下面的路线:

about_landings GET  /landings/about(.:format)  landings#about 
landings  GET  /landings(.:format)    landings#index 
       POST /landings(.:format)    landings#create 
new_landing  GET  /landings/new(.:format)   landings#new 
edit_landing GET  /landings/:id/edit(.:format) landings#edit 
landing   GET  /landings/:id(.:format)   landings#show 
       PATCH /landings/:id(.:format)   landings#update 
       PUT  /landings/:id(.:format)   landings#update 
       DELETE /landings/:id(.:format)   landings#destroy 

我只需要大约路线,以及可能的几个其他静态页面路由。这是什么routes.rb语法?

回答

1

你可以做些什么如:

resources :landings, only: [] do 
    collection do 
    get 'about' 
    end 
end 

,这将给你:

about_landings GET /landings/about(.:format) landings#about 

就个人而言,我不喜欢about_landings为路径名(美学),所以我想我会做的事:

scope module: :landings do 
    get 'about' 
end 

,这将给你:

about GET /about(.:format) landings#about 

然后你可以只使用about_path这IMO是更好的(和你同时建立输入更少的字符10,从而为您的整个寿命增加小数秒)。此外,您还可以在浏览器地址栏中获得更干净的(又是IMO)网址。

+0

是的,这是我正在寻找的。我同意,模块路线是“更清洁” – EastsideDeveloper

+0

太棒了!希望对你有效。如果您觉得合适,请随时上传/接受。 – jvillian

3

除/只

resources :landings, except: [:show, :new, :edit] do 
    collection do 
    get 'about' 
    end 
end 

OR

resources :landings, only: [:index] do 
    collection do 
    get 'about' 
    end 
end 

注意您可以使用:您可以跳过或只允许特定action.I刚刚给出的例子

+0

是的,这个工程。我想知道是否有语法,这将禁用所有的默认路由,而不必单独列出它们。 – EastsideDeveloper