0

I've a controller concern which regroup the logic for access control:

module AccessControl
  extend ActiveSupport::Concern

  def restrict
    unless current_user
      flash.now[:danger] = 'You must log in first'
      redirect_to :root
    end
  end
end

which is included in some controllers and used as before_action filter

class SomeController < ApplicationController
  include AccessControl

  before_action :restrict
end

But somehow the flash message is not showing up (other flash messages are properly displayed if they're called from Controllers).

Any idea about why I can't call a flash message from a Concern ? If it's not the expected/recommended implementation, how could I've DRY controllers but still have flash messages displayed ?

Haven't found the answer inside ActiveSupport::Concern's nor ActiveDispach::Flash's documentation. I'm trying some alternatives but none of them are actually working.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sumak
  • 927
  • 7
  • 21

2 Answers2

1

Just test your code, it works.

From your code, the flash message only displays when a user is not signed in, so make sure sign out when you test.

eux
  • 3,072
  • 5
  • 14
  • Indeed, I didn't know I'd to restart the server when including concerns. Thanks for your time & answer ! – Sumak Mar 05 '21 at 11:04
0

AJP's answer solved my issue.

TLDR: use flash[:danger] with redirect_to

Sumak
  • 927
  • 7
  • 21