LoginSignup
6
5

More than 3 years have passed since last update.

RailsでGenerateしたらArgumentErrorで怒られた話

Posted at

ルー大柴みたい

Railsチュートリアルも完走したし、オリジナルWebアプリ作るぞーと意気込んでGenerateしてみたところ、
ArgumentErrorと怒られたのでその備忘録です。

$ rails generate controller Companys show
Traceback (most recent call last):
        58: from bin/rails:4:in `<main>'
        〜〜省略〜〜
         1: from /Library/Ruby/Gems/2.6.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/mapper.rb
            :307: in `check_controller_and_action'
/Library/Ruby/Gems/2.6.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/mapper.rb
:327:in `check_part': Missing :controller key on routes definition, please check your routes. 
(ArgumentError)

ArgumentErrorとは

PixelSnap 2020-02-18 at 07.41.04@2x.png

メソッドの引数が正しくない時や足りない時に発生するエラーです。

wrong number of arguments (given 0, expected 1+) というエラーメッセージが表示されています。
これは 引数の数が間違っています(現状0個です、正常な引数の数は1個以上です) という意味です。
つまり form_for のメソッドの引数が足りないために起こっているエラーだということが分かります。

メソッドのリファレンス(http://railsdoc.com/)を見ながら、正しい引数が設定されいるかを確認し修正すると解決できることが多いです。

参考記事「エラーメッセージから学ぶ」

なるほど、ArgumentErrorは引数のエラーなんですね
そしてエラー文を読み込むのが大事と。

エラーメッセージに立ち返る

Missing :controller key on routes definition, please check your routes. (ArgumentError)
「routes上にあるコントローラーのキーが未定義ですので、routesを確認してね。」

なんかよくわからんけど、please check your routes.って言われたし仕方ねえ確認してやるか。

routes.rb
Rails.application.routes.draw do
  root 'static_pages#home'
  get ''  # <= 何これ?
end

とりあえず削除してみて、もう一度generateしてみる

$ rails generate controller Companys show
      create  app/controllers/companys_controller.rb
       route  get 'companys/show'
      invoke  erb
      create    app/views/companys
      create    app/views/companys/show.html.erb
      invoke  test_unit
      create    test/controllers/companys_controller_test.rb
      invoke  helper
      create    app/helpers/companys_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/companys.coffee
      invoke    scss
      create      app/assets/stylesheets/companys.scss

上手く行った!!!

結論:作業途中のコードを放置するのはやめよう

そう心に誓いました。

6
5
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
6
5