1

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
Community
  • 1
  • 1
Justin Meltzer
  • 13,318
  • 32
  • 117
  • 182

1 Answers1

0

I'm not sure if there is a better way of doing this, however when you in your access deny method, you can do something like:

def access_denied
    notices = {
      :vote => {:new => "You must log in before voting"},
      :video => {:destroy => "You must log in before deleting a video"} 
    }
    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

Where your notices hash will have the keys as the symbols of the controller name, and the values will be hashes of keys of the action name's symbol to the value of the error message.

You will then check against this hash in setting :notice, and if it doesn't exist in the notices hash, it will default to "You must be log in to preform..."

From there you can grab the notice in the sessions#new method.

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • hmmm, would you recommend either one? – Justin Meltzer Mar 25 '11 at 04:17
  • I'd recommend the first option, in which you don't allow the user to access the login page if he/she is already logged in. Unless of course your system allows you to have multiple accounts, otherwise... I'd go with option one. – Mike Lewis Mar 25 '11 at 04:17
  • Actually, I don't think this will work perfectly. Won't the flash message appear whenever the user is not logged in and accesses the login page, even if he's not voting? This shouldn't happen. – Justin Meltzer Mar 25 '11 at 04:20
  • thanks, but it doesn't seem to work... unless I'm doing something wrong – Justin Meltzer Mar 25 '11 at 04:47
  • ok posted the code... nothing happens, no error but the flash message does not appear – Justin Meltzer Mar 25 '11 at 04:51
  • When you are redirected to sessions#new, does the notice param get set correctly? – Mike Lewis Mar 25 '11 at 04:53
  • You should keep controller_name.to_sym and action_name.to_sym. These are actual methods that you can call which will return the current controller/action you are on. – Mike Lewis Mar 25 '11 at 04:54
  • so that would be the video_votes controller and the create action? or the sessions controller and the new action? – Justin Meltzer Mar 25 '11 at 04:56
  • that would be the video_votes controller and the create action, as this was called in the before_filter in the video_votes, correct? – Mike Lewis Mar 25 '11 at 04:57
  • yup correct, although then the new.js.erb file in my sessions directory is called... which kind of confuses me... – Justin Meltzer Mar 25 '11 at 04:57
  • ok, updated to the code i'm using, although the flash message doesn't appear, and I don't see the notice params in my logs – Justin Meltzer Mar 25 '11 at 05:00
  • change the redirect line to `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` – Mike Lewis Mar 25 '11 at 05:07
  • should the end just be `return` or `return false` – Justin Meltzer Mar 25 '11 at 05:12
  • No, I am passing the :notice parameter into login_path now (which is how you pass params) – Mike Lewis Mar 25 '11 at 05:12
  • ok I don't see the flash message, but I do see this in my logs: `Started GET "/login?notice=You+must+log+in+to+perform+this+action."` – Justin Meltzer Mar 25 '11 at 05:14
  • So from there you can grab the params[:notice] and set the error message. i.e. flash.now[:alert] = params[:notice] – Mike Lewis Mar 25 '11 at 05:15
  • `flash.now[:alert] = params[:notice]` in the new action of the session controller? it doesn't work... the flash message doesn't appear – Justin Meltzer Mar 25 '11 at 05:33