0

I'm sure that there is an answer to this out there, but I'm not entirely certain how to properly phrase this question, so my apologies if this is repetitious.

I am working on implementing a badge/achievement system for a site. The backend stuff is there, but I'm working on the front-end now, and I'm basically trying to figure out how to redirect to a sort of "Congratulations!" page when someone gets a new badge.

The congratulations page is going to be a modal, but for simplicities sake, if anyone has an idea of how to trigger an action like this only once when a new Badge is created, that would be a huge help. Right now, when a user performs a given action, say... adding money to their account, a user_badge is created (adds a Badge ID to an array).

Thanks in advance!

1 Answers1

0

You can use before_filter for actions, where you want to check that new badge appears (probably you don't want to check it in auth actions, so you can't use global before_filter). Then you can use redirect_to in this before filter, if user has a new badge.

But I don't recommend to do so, since you'll break the users flow. It's better to show alert/modal on the next requested page. To achieve this you can define a new instance variable in your before_filter (like @show_modal) and include the partial with modal to your footer.

example:

#before filter
def check_for_new_badges 
  if current_user.has_new_badges?
    @show_modal = true
    current_user.set_badges_as_seen!
  end
end

#included in layout
- if @show_modal
  = render 'shared/congrats_modal'
Mik
  • 4,117
  • 1
  • 25
  • 32