Closed
Description
RuboCop reports a false positive with if/else and redirect_to
Follow up from #812 (comment)
Expected behavior
Rubocop does not report errors
Actual behavior
Inspecting 1 file
C
Offenses:
pages_controller.rb:6:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:success] = t('.flash.success') # false positive
^^^^^
pages_controller.rb:17:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:notice] = t('.flash.notice') # false positive
^^^^^
pages_controller.rb:25:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:success] = t('.flash.success') # false positive
^^^^^
pages_controller.rb:27:7: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:alert] = t('.flash.fail') # false positive
^^^^^
1 file inspected, 4 offenses detected, 4 offenses autocorrectable
Steps to reproduce the problem
- Create the following
pages_controller.rb
file
# frozen_string_literal: true
class PagesController < ApplicationController
def update
if @page.save
flash[:success] = t('.flash.success') # false positive
else
flash.now[:alert] = t('.flash.fail')
render 'edit'
end
redirect_to @page
end
def dismiss
if @something
flash[:notice] = t('.flash.notice') # false positive
end
redirect_to pages_path
end
def destroy
if @page.destroy
flash[:success] = t('.flash.success') # false positive
else
flash[:alert] = t('.flash.fail') # false positive
end
redirect_to pages_path
end
end
- Run
rubocop --require rubocop-rails --only Rails/ActionControllerFlashBeforeRender pages_controller.rb
RuboCop version
$ rubocop --require rubocop-rails -V
1.37.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [x86_64-darwin21]
- rubocop-rails 2.17.0
Also tested against rubocop-rails main
Activity