resources :'articles:リソース名s'
routes.rb
resources :articles
| URL | アクション | HTTP メソッド |
|---|---|---|
| /members(.:format) index GET 一覧画面を表示 | ||
| /members/:id(.:format) show GET 個別の詳細画面を表示 | ||
| /members/new(.:format) new GET 新規データの登録画面を表示 | ||
| /members(.:format) create POST 新規データの登録画面からの入力を受けて登録画面を表示 | ||
| /members:id/edit(.:format) edit GET 既存のデータの編集画面を表示 | ||
| /members/(.:format) update PUT 編集画面からの入力を受けて更新処理 | ||
| /members(.:format) destroy DELETE 一覧画面で選択されたデータを削除処理 |
| ヘルパー名(_path) | ヘルパー名(_url) | 戻り値(パス ) |
|---|---|---|
| members_path members_url /members | ||
| members_path(id) memvers_url(id) /members/:id | ||
| new_member_path new_members_url /members/new | ||
| edit_member_path(id) edit_members_url(id) /members/:id/edit |
resource :'config:リソース名'
routes.rb
# アプリケーション設定(config)の取得・編集のルート設定
resource :config
| URL | アクション | HTTP メソッド |
|---|---|---|
| /config(.:format) show GET | ||
| /config/new(.:format) new GET | ||
| /config(.:format) create POST | ||
| /config/edit(.:format) edit GET | ||
| /config(.:format) update PUT | ||
| /config(.:format) destroy DELETE |
| ヘルパー名(_path) | ヘルパー名(_url) | 戻り値(パス ) |
|---|---|---|
| config_path config_url /config /config | ||
| new_config_path new_config_url /config/new | ||
| edit_config_path edit_config_url /config/edit |
resouces :'articles:リソース名s' do
routes.rb
# articles/commentsの親子関係をリソース定義
resouces :article do
resouces :comments
end
resources :articles, :constraints(:'id:paramパラメータ名' => '/[0-9]{1,2}/:制約条件正規表現regexp') do
view_controller.rb
# articlesリソースのidパラメーターは1~2桁の数値であること
resources :articles, :constraints => { :id => /[0-9]{1,2}/ }
# comments/usersリソースのidパラメータは1~3桁の数値であること
constraints(:id: => /[0-9]{1,2}/) do
resources :comments
resources : users
end
resources :'articles:リソース名' do
collection do 'get:関連付けるHTTPメソッド' 'unlocked:関連付けるアクション'
end
routes.rb
resources :articles do
# 複数オブジェクトを扱うuncheckedアクションを追加
collection do
get 'unlocked'
end
# 単一オブジェクトを扱うdraftアクションを追加
member do
get 'draft'
end
end
-----------
>rake routes
# ルート表示
resources :articles do
'get:関連付けるHTTPメソッド' 'unchecked:関連付けるアクション', :on => 'collection:アクションの種類'
end
routes.rb
routes.rb
resources :articles do
# 複数オブジェクトを扱うuncheckedアクションを追加
get 'unchecked', :on => collection
# 単一オブジェクトを扱うdraftアクションを追加
get ''draft, :on => :member
end
---------
>rake routes
# ルート表示
resources :'articles:リソース名', :expect/only => "['show', 'edit', 'update', 'destroy']:除外/残すアクション"
routes.rb
# articles.rb
resources :articles, :expect => ['show', 'edit', 'update', 'destroy']
# usersリソースで自動生成するアクションを指定
resources :users, :only => ['show', 'new', 'create']
---------
>rake routes
# ルート表示
resources :articles, :path_names => { :new => 'regist:insertアクション名', :edit => 'renew:editアクション名' }
routes.rb
resources :articles, :path_names => { :new => 'regist', :edit => 'renew' }
---------
>rake routes
# ルート表示
resources :'comments:リソース名', :as => 'reviews:コントローラ名'
routes.rb
# commentsリソースをReviewsControllerにマッピング(本来はCommentsController)
resources :commits, :controller => 'reviews'
resources :'comments:リソース名', :as => 'reviews:ヘルパー名'
routes.rb
# reviews_path/review_pathのようなヘルパーを生成
# (本来はcomments_path、comment_pathなどを生成)
resources :comments, :as => 'reviews'