Search code examples
ruby-on-railsruby-on-rails-4devisealertbefore-filter

How to add a notice/alert to a before_filter


So I have a before filter like the following when using devise:

before_action :authenticate_user!, only: [:new, :destroy, :edit, :update]

So whenever I go to the new action I an redirected to users_sign_in route if I am not signed in. However the user might be confused and would like to know why they are being redirected there when they intended to use the new action.

How can I add a message or an alert to a before filter that shows up only if there is a redirect...

Something like:

notice: 'You must sign in before creating a new product!'

Solution

  • I ended up finding a better solution while testing different things. I think it's simpler and more intuitive:

    before_action :authenticate_user!, only: [:new, :destroy, :edit, :update], notice: 'you must sign in first!'