So when I'm logged out and I try to vote, I am redirected to my login page. Check out this question I asked for more details on what my code looks like: Redirect not working
Now I implemented a new.js.erb file in my sessions directory (I followed the accepted answer in the question I linked to) which does the redirecting. However, I now want to add a custom flash message upon being redirected to the page, something like "You must be logged in to vote."
I tried adding this line:
flash.now[:alert] = "You must be logged in to vote."
to the new action of my sessions controller, but this adds the message whenever I go to the login page, even if I'm already logged in. What should I do?
This is the code I'm using:
def access_denied
notices = {
:video_vote => {:new => "You must log in before voting"}
}
controller_name_sym = controller_name.to_sym
action_name_sym = action_name.to_sym
redirect_to login_path, :notice => (notices[controller_name_sym] && notices[controller_name_sym][action_name_sym] || "You must log in to perform this action.") and return false
end