1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Conditionally use helper_method in Flash concern

I was attempting to use the `flash` functionality in a `Metal`
controller. When including the `flash` concern I received the following
error:

    NoMethodError: undefined method `helper_method'....

Either:

- `AbstractController::Helpers` should be a dependency of
  `ActionController::Flash`
- `ActionController::Flash` should not require the existence of
  `AbstractController::Helpers`.

Since my use case (set a flash and redirect) has no need for the helper
method and that is a common use case, making the dependency conditional
seemed the better option.

NOTE: This is similar to issue #21067 only the error is within Rails
itself while that issue had the error within Devise.
This commit is contained in:
Eric Anderson 2018-08-09 12:00:23 -04:00
parent 3000c14905
commit 34326af1d4
No known key found for this signature in database
GPG key ID: 67BE209ABD22DB7C
2 changed files with 13 additions and 3 deletions

View file

@ -33,10 +33,12 @@ module ActionController #:nodoc:
types.each do |type|
next if _flash_types.include?(type)
define_method(type) do
request.flash[type]
if respond_to? :helper_method
define_method(type) do
request.flash[type]
end
helper_method type
end
helper_method type
self._flash_types += [type]
end

View file

@ -342,6 +342,14 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
end
end
def test_flash_usable_in_metal_without_helper
assert_nothing_raised do
Class.new ActionController::Metal do
include ActionController::Flash
end
end
end
private
# Overwrite get to send SessionSecret in env hash