Closed
Description
Sorry if this is a duplicate. I was trying to make sure all possible fixes were released before adding this bug report.
We get a Rails/ActionControllerFlashBeforeRender
error when the if
block does a redirect_to
and return
before the render
block (see below for setup).
Expected behavior
No error should be triggered for the cop Rails/ActionControllerFlashBeforeRender
.
Actual behavior
It triggers Rails/ActionControllerFlashBeforeRender
when it shouldn't.
Steps to reproduce the problem
I have an admin controller:
# frozen_string_literal: true
module Admin
class ListingAttributeTypesController < ApplicationController
before_action :find_listing_attr_type, only: %i[edit update]
# :nodoc:
def index
@listing_attribute_types = ListingAttributeType.ord_inactive.ord_name
end
# :nodoc:
def new
@listing_attribute_type = ListingAttributeType.new
render('form')
end
# :nodoc:
def create
@listing_attribute_type = ListingAttributeType.new(listing_attr_type_params)
if @listing_attribute_type.save
flash[:success] = 'Listing attribute type saved!'
redirect_to(admin_listing_attribute_types_path)
return
end
render('form', status: :unprocessable_entity)
end
# :nodoc:
def edit
render('form')
end
# :nodoc:
def update
if @listing_attribute_type.update(listing_attr_type_params)
flash[:success] = 'Listing attribute type updated!'
redirect_to(admin_listing_attribute_types_path)
return
end
render('form', status: :unprocessable_entity)
end
private
# Return safe form params
#
# @return [ActionController::Parameters]
#
def listing_attr_type_params
params.require(:listing_attribute_type).permit(:id, :name, :inactive)
end
# Finds the listing attribute type
#
# @return [void]
#
def find_listing_attr_type
@listing_attribute_type = ListingAttributeType.find(params[:id])
end
end
end
I then run rubocop
and get the following:
Offenses:
app/controllers/admin/listing_attribute_types_controller.rb:24:9: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:success] = 'Listing attribute type saved!'
^^^^^
app/controllers/admin/listing_attribute_types_controller.rb:42:9: C: [Correctable] Rails/ActionControllerFlashBeforeRender: Use flash.now before render.
flash[:success] = 'Listing attribute type updated!'
^^^^^
RuboCop version
# ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]
# rubocop -V
1.42.0 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.1.2) [aarch64-linux]
- rubocop-rails 2.17.4
# bundle exec rubocop -V
1.42.0 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.1.2) [aarch64-linux]
- rubocop-rails 2.17.4
Activity