Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails/ActionControllerFlashBeforeRender reports false positives with multiline if/else, redirect_to #843

Closed
tagliala opened this issue Oct 27, 2022 · 0 comments · Fixed by #844
Labels
bug Something isn't working

Comments

@tagliala
Copy link
Contributor

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

  1. 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
  1. 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

@koic koic added the bug Something isn't working label Oct 27, 2022
koic added a commit to koic/rubocop-rails that referenced this issue Oct 27, 2022
…shBeforeRender`

Fixes rubocop#843.

This PR fixes a false positive for `Rails/ActionControllerFlashBeforeRender`
when using `flash` in the multiline statements as `if` branch before `redirect_to`.
koic added a commit to koic/rubocop-rails that referenced this issue Oct 27, 2022
…shBeforeRender`

Fixes rubocop#843.

This PR fixes a false positive for `Rails/ActionControllerFlashBeforeRender`
when using `flash` in multiline `if` branch before `redirect_to`.
@koic koic closed this as completed in #844 Oct 27, 2022
koic added a commit that referenced this issue Oct 27, 2022
…on_controller_flash_before_render

[Fix #843] Fix a false positive for `Rails/ActionControllerFlashBeforeRender`
rnestler added a commit to renuo/applications-setup-guide that referenced this issue Jan 23, 2023
This cop seems to have quite a lot false positives pop up every now and
then which are actually harmful since we won't render the flash in these
cases.

See rubocop/rubocop-rails#843 and
renuo/vdrb-kas@7978dd7
rnestler added a commit to renuo/applications-setup-guide that referenced this issue Jan 23, 2023
This cop seems to have quite a lot false positives pop up every now and
then which are actually harmful since we won't render the flash in these
cases.

See rubocop/rubocop-rails#843 and
renuo/vdrb-kas@7978dd7
rnestler added a commit to renuo/applications-setup-guide that referenced this issue Mar 2, 2023
This cop seems to have quite a lot false positives pop up every now and
then which are actually harmful since we won't render the flash in these
cases.

See rubocop/rubocop-rails#843 and
renuo/vdrb-kas@7978dd7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants