LoginSignup
15
14

More than 5 years have passed since last update.

【Rails5】はじめてのプロジェクトでRubocopから指摘されたエラーのまとめ

Posted at

はじめに

はじめてのRailsプロジェクトで作成したプルリクエストで、Rubocopから指摘された事項をまとめます。

Rubocopの実行コマンド

$ bundle exec rubocop

指摘事項

Use %i or %I for an array of symbols.

指摘例

app/controllers/hoge1s_controller.rb:4:36: C: Use %i or %I for an array of symbols.
  before_action :new_hoge1, only: [:index, :new]
                                   ^^^^^^^^^^^^^^

詳細

配列を指定する際に、%iまたは%Iがない。

【参考】Ruby 2.4.0 リファレンスマニュアル > リテラル %記法

  • %i
    • 要素がシンボルの配列(空白区切り)
  • %I
    • 要素がシンボルの配列(空白区切り)。式展開、バックスラッシュ記法が有効

修正内容

配列を指定するときに、%iまたは%Iをつける。
rb
before_action :new_hoge1, only: %i[:index, :new]

':' and ',' are unnecessary and may be unwanted in the resulting symbols.

指摘例

app/controllers/hoge1s_controller.rb:4:36: W: Within %i/%I, ':' and ',' are unnecessary and may be unwanted in the resulting symbols.
  before_action :new_hoge1, only: %i[:index, :new]

詳細

':'と '、'は不要であり、結果として生じるシンボルで望ましくないかもしれません。

修正内容

配列内から:および、,を削除。

  before_action :new_hoge1, only: %i[index new]

Put empty method definitions on a single line.

指摘例

  def index
  end

詳細

空のメソッドは1行で定義する。

修正内容

空のメソッドは1行で定義する。
また、アクションの後ろにセミコロンを付与する。

  def index; end

Keep a blank line before and after private.

指摘例

app/controllers/hoge1s_controller.rb:47:3: C: Keep a blank line before and after private.
  private
  ^^^^^^^

詳細

privateキーワードの前後には空白行を入れる。

修正内容

詳細の通り。

Inconsistent indentation detected.

指摘例

app/controllers/hoge1s_controller.rb:48:5: C: Inconsistent indentation detected.
    def new_hoge1 ...
    ^^^^^^^^^^^^^^

詳細

インテンドの不一致。

今回は、privateメソッドのインテンドが不一致だと指摘されていました。
Railsチュートリアルの記載方法に習いましたが、Rubocop的にはダメなんですね。
(privateメソッドの下は1段下げておいた方が見やすくないか???)

  private

    def new_hoge1
      @hoge1 = hoge1.new
    end

修正内容

インテンドを半角スペース4つから、半角スペース2つに修正。
また、privateキーワード以下がprivateメソッドであることが分かるように記号を追加。
(先輩からのアドバイスに習いましたが、世の中的にどうなんだろう?)

  private ###################################################################################################

  def new_hoge1
    @hoge1 = hoge1.new
  end

Indent ) the same as the start of the line where ( is.

指摘例

app/controllers/hoge1s_controller.rb:57:9: C: Indent ) the same as the start of the line where ( is.
        )
        ^

詳細

)のインテンドは、先頭の(と同じにする。

    params.require(:hoge1).permit(
      :hoge1_code,
      :hoge1_name_ja,
      :hoge1_name_en
      )

修正内容

)のインテンド位置を、先頭の(と同じになるように修正。

    params.require(:hoge1).permit(
      :hoge1_code,
      :hoge1_name_ja,
      :hoge1_name_en
    )

Extra empty line detected at class body end.

指摘例

app/controllers/hoge1s_controller.rb:75:1: C: Extra empty line detected at class body end.
app/models/hoge1.rb:8:62: C: Space inside } missing.
                          uniqueness: { case_sensitive: false}
                                                             ^

詳細

クラス本体の最後で余計な空行が検出された。

  def set_order_move
    self.move_order_item = hoge1
  end

end

修正内容

最後のendの前にある余計な空行を削除する。

  def set_order_move
    self.move_order_item = hoge1
  end
end

Align the elements of a hash literal if they span more than one line.

指摘例

app/models/hoge1.rb:10:25: C: Align the elements of a hash literal if they span more than one line.
                        presence: true
                        ^^^^^^^^^^^^^^

詳細

ハッシュリテラルの要素が複数の行にまたがっている場合は、それらの要素をそろえる。

  validates :hoge1_name_ja, length: { maximum: 50 },
                        presence: true

修正内容

EnforcedColonStyle key(デフォルト) でチェックしているため、キーを左揃えにする。

  validates :hoge1_name_ja, length: { maximum: 50 },
                             presence: true

Final newline missing.

指摘例

app/decorators/hoge1_decorator.rb:4:4: C: Final newline missing.
end

詳細

最終行に改行がない。

修正内容

最終行に改行を追加する。

Space inside } missing.

指摘例

app/models/hoge1.rb:8:62: C: Space inside } missing.
                          uniqueness: { case_sensitive: false}
                                                             ^

詳細

内側のスペースがない。

修正内容

falseの後ろにスペースを追加。

  validates :hoge1_code, length: { maximum: 15 },
                          presence: true,
                          uniqueness: { case_sensitive: false }
15
14
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
15
14