Closed
Description
RuboCop reports a false positive with if/else and redirect_to
Follow up from #825
Expected behavior
Rubocop does not report errors
Actual behavior
Inspecting 1 file
C
Offenses:
rubocop_rails_825.rb:8:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:success] = t('.flash.success') # false positive
^^^^^
rubocop_rails_825.rb:12:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:alert] = t('.flash.fail') # false positive
^^^^^
1 file inspected, 2 offenses detected, 2 offenses autocorrectable
Steps to reproduce the problem
- Create the following
rubocop_rails_825.rb
file
# frozen_string_literal: true
class PagesController < ApplicationController
def destroy
if @page.destroy
do_something
flash[:success] = t('.flash.success') # false positive
else
do_something_else
flash[:alert] = t('.flash.fail') # false positive
end
redirect_to pages_path
end
end
- Run
bundle exec rubocop --require rubocop-rails --only Rails/ActionControllerFlashBeforeRender rubocop_rails_825.rb
RuboCop version
$ bundle exec rubocop --require rubocop-rails -V
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.0.4) [x86_64-darwin22]
- rubocop-rails 2.17.2
Also tested against rubocop-rails main
Activity