Skip to content

Rails/ActionControllerFlashBeforeRender reports false positives with if/else and redirect_to #825

Closed
@tagliala

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions