2
2

More than 5 years have passed since last update.

Rails3のルーティングメモ

Last updated at Posted at 2012-08-21

Rails2とルーティングの仕方が変わっているようなのでメモ。

1. public/index.htmlを消しておく

mv ./app/public/index.html ./app/public/index.html.backup

2.ルーティングの設定

./config/routes.rbを編集する。
上に書くほど優先順位が高くなる。

ルートコントローラの設定

以下のように設定すると、labelsコントローラのnewアクション画面がデフォルトページとなる。

config/routes.rb
root :to => 'labels#index'

Scaffoldを利用した場合

resourcesが追記される。
Rails2のmap.resourcesであり、自動でCRUDに沿ったルーティングルールを作成してくれるので便利。
これの下にmatchルールを書いても、こちらが優先されるので注意。

config/routes.rb
resources :labels

matchを使った設定

matchルールで様々なパターンのルーティングができる。
例えば以下のように書くと、http://〜/feedbackというURLでは、feedbacksコントローラのnewアクションを表示する。
それ以外はpagesコントローラを参照する。

config/routes.rb
match "feedback" => 'feedbacks#new'
match "pages(/:action)" => 'pages#:action'
match "(:action)" => 'pages#:action'
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2