0

My flash message doesn't show when I use i18n, but does when I just use a string. This is in the application controller.

This works:

def user_not_authorized(exception)
    policy_name = exception.policy.class.to_s.underscore
    ## require 'pry'; binding.pry
flash[:alert] = "You're not invited for this event."
    redirect_to request.referrer || root_path
end

This doesn't show anything:

def user_not_authorized(exception)
    policy_name = exception.policy.class.to_s.underscore
    ## require 'pry'; binding.pry
    flash[:error] = I18n.t "#{policy_name}.#{exception.query}", scope: 'pundit',
                       default: [:default, "I'm sorry, you're not allowed"]
    redirect_to request.referrer || root_path
end

I tried using just t instead of I18n.t but no effect either.

When using pry and running the I18n.t it shows the expected String.

The i18n file:

pundit:
  default: "I'm sorry, but you're not allowed to see this."
  event_policy:
    show?: 'You cannot view this event. You need an invitation first.'

What am I doing wrong?

puredevotion
  • 1,135
  • 1
  • 11
  • 27
  • 2
    You have `flash[:alert]` on the first code and `flash[:error]` on the second one, does using `:alert` fixes your issue? – arieljuod Feb 17 '19 at 19:49
  • @arieljuod wow. 2hours in, and this is the fix! – puredevotion Feb 17 '19 at 19:53
  • 1
    I'd recommend you to read about the Flash hash, you can use :error if you want, just be sure to update the part of your layout that renders that https://guides.rubyonrails.org/action_controller_overview.html#the-flash – arieljuod Feb 17 '19 at 19:55
  • Thanks. I've been Ruby'ing for a short while now. Lots of manual parts to read! – puredevotion Feb 17 '19 at 20:06

0 Answers0