You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classHomeController < ApplicationControllerdefupdateifconditionflash[:danger]="msg"redirect_toroot_pathandreturnendifother_conditionflash[:success]="other msg"redirect_toroot_pathelseflash.now[:danger]="other other msg"render'something',status: :unprocessable_entityendendend
Actual behavior
Rubocop says to use flash.now before render, which I think is wrong:
def update
if condition
flash[:danger] = "msg"
+ ^^^^^ Use `flash.now` before `render`.
redirect_to root_path and return
end
Steps to reproduce the problem
--- a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb
+++ b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb
@@ -328,4 +328,27 @@ RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config d
RUBY
end
end
+
+ context 'when using `flash` before `render` and returning `redirect_to` in condition block - part deux' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~RUBY)
+ class HomeController < ApplicationController
+ def update
+ if condition
+ flash[:danger] = "msg" # breaks here
+ redirect_to root_path and return
+ end
+
+ if other_condition
+ flash[:success] = "other msg"
+ redirect_to root_path
+ else
+ flash.now[:danger] = "other other msg"
+ render 'something', status: :unprocessable_entity
+ end
+ end
+ end
+ RUBY
+ end
+ end
Spec passes if I change the example code to use return redirect_to root_path inside the offending condition so I suspect it's related to the usage of redirect_to ... and return. However, the spec will pass when using redirect_to root_path and return if I remove the code starting with if other_condition through the end of the Rails action.
Expected behavior
This code should be valid:
Actual behavior
Rubocop says to use
flash.now
before render, which I think is wrong:Steps to reproduce the problem
Spec passes if I change the example code to use
return redirect_to root_path
inside the offending condition so I suspect it's related to the usage ofredirect_to ... and return
. However, the spec will pass when usingredirect_to root_path and return
if I remove the code starting withif other_condition
through the end of the Rails action.RuboCop version
bundle exec rubocop -V
1.63.1 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.1.4) [arm64-darwin22]
The text was updated successfully, but these errors were encountered: