LoginSignup
0
0

More than 5 years have passed since last update.

railsのroutes.rbの書き方はちゃんと覚えようって話

Last updated at Posted at 2019-02-16

環境

  • cloud9
  • Rails 5.2.2

routes.rbを書き換えたかった

よくある「/」に対するルーティングを書きたかった。
以下の例では「home/top」を「/」にしたかった。

Rails.application.routes.draw do
  get 'sing/in'
  get 'sing/out'
  get 'sing/up'
  get '/' => 'home/top'
  get 'home/about'
  get 'card/show'
  get 'list/show'
  get 'list/add'
end

ほいほい、それじゃあ実行してこー。

$ rails server
=> Booting Puma
=> Rails 5.2.2 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
/usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.2.2/lib/action_dispatch/routing/mapper.rb:329:in `check_part': Missing :action key on routes definition, please check your routes. (ArgumentError)
        from /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.2.2/lib/action_dispatch/routing/mapper.rb:318:in `check_controller_and_action'
  (以下略)

どーーぉしてーえーえぇー!(サカナクションのアイデンティティっぽく)
「=>」を「->」に変えて「それシンタックスエラーやで」と怒られてみたり、矢印の向きを逆にしてみたりといろいろやって5分経過。

解:「/」を「#」に変更

正しい書き方はこちら。

Rails.application.routes.draw do
  get 'sing/in'
  get 'sing/out'
  get 'sing/up'
  get '/' => 'home#top'
  get 'home/about'
  get 'card/show'
  get 'list/show'
  get 'list/add'
end

なんでこんなことでハマってんだと自己嫌悪になりながら、自戒を込めてメモメモ。

0
0
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
0
0