手把手教你学习ROR-6.Rooter的配置
Router的好处
1 能使你的URL更符合REST规范,而不是带着参数的URL
2 能让你的View使用Path,而不是直接硬编码
3 统一放置,方便管理
Router,View的对用关系
GET | /photos | index | display a list of all photos | photos_path |
GET | /photos/new | new | return an HTML form for creating a new photo | new_photo_path |
POST | /photo | create | create a new photo | photo_path |
GET | /photos/:id | show | display a specific photo | photo_path(@photo) |
GET | /photos/:id/edit | edit | return an HTML form for editing a photo | edit_photo_path(@photo) |
PATCH/PUT | /photos/:id | update | update a specific photo | photo_path(@photo) |
DELETE | /photos/:id | destroy | delete a specific photo | photo_path(@photo) |
Router的使用
A: NameSpace
namespace
:group
do
resources
:articles
end
这里使用Group::ArticleController,而路径/group/article/:id之类的group_articles_path
B: Scope
scope
'/admin'
do
resources
:posts
,
:comments
end
这里使用ArticleController,路径/group/article/:id之类的
C: Module
resources :articles, module: 'group'
same with;
scope module: 'group' do
resources :articles
end
这里使用Group::ArticleController,路径/article/:id之类的
D:嵌套
resources :magazines do
resources :ads
end
这里使用 AdsController,路径/magazines/:magazine_id/ads,magazine_ads_url
E: Concern
concern
:commentable
do
resources
:comments
end
resources
:messages
, concerns:
:commentable
这里使用CommentsController,路径/messages/:message_id/comments
F: Shallow