Class: RuboCop::Cop::Rails::ActionControllerFlashBeforeRender

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/action_controller_flash_before_render.rb

Overview

Using ‘flash` assignment before `render` in Rails controllers will persist the message for too long. Check guides.rubyonrails.org/action_controller_overview.html#flash-now

Examples:


# bad
class HomeController < ApplicationController
  def create
    flash[:alert] = "msg"
    render :index
  end
end

# good
class HomeController < ApplicationController
  def create
    flash.now[:alert] = "msg"
    render :index
  end
end

Constant Summary collapse

MSG =
'Use `flash.now` before `render`.'
RESTRICT_ON_SEND =
[:flash].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(flash_node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/rails/action_controller_flash_before_render.rb', line 53

def on_send(flash_node)
  return unless flash_assignment?(flash_node)

  return unless followed_by_render?(flash_node)

  return unless instance_method_or_block?(flash_node)

  return unless inherit_action_controller_base?(flash_node)

  add_offense(flash_node) do |corrector|
    corrector.replace(flash_node, 'flash.now')
  end
end