0

When a user is not logged in and tries to vote, I want to prompt the user to log in.

You can check out this previous question I asked to get a sense of what my code is like: Redirect not working

How would I, instead of redirecting the user to the login page, render with AJAX a div or a modal dialogue that has a form in it to login, or at least a link in it to take the user to the login page...

Of course this div should be rendered only when the user is not logged in and tries to vote....

Community
  • 1
  • 1
Justin Meltzer
  • 13,318
  • 32
  • 117
  • 182

2 Answers2

1

I had implemented this using facebox_render (https://github.com/ihower/facebox_render)

Install and configure facebox_render and procede..

There was a before_filter in my application controller

def authorized?
  if not user_signed_in?
    respond_to do |format|
      format.html do
        flash[:error] = "Sorry!!! You are not authorized to access that page!!!"
        redirect_to root_path
      end

      format.js do
        render_to_facebox(:template => dashboards_authorization_failed_path)
      end
    end
  end
end

dashboards_authorization_failed_path directs to {:controller => 'dashboards', :action => 'authorization_failed'}

Finally in authorization_failed.js.erb

<div class='flash-error facebox_err'>
  Sorry!!! You are not authorized to access that page!!!
</div>

This displays the message "Sorry!!! You are not authorized to access that page!!!" in a facebox

You can have a form or link to sign_in page in the facebox, simply put your code in the js.erb

Pravin
  • 6,592
  • 4
  • 42
  • 51
  • thanks! this is helpful. `dashboards_authorization_failed_path` is the named route... is that right? – Justin Meltzer Mar 25 '11 at 06:35
  • Here it is.. `match 'dashboards/authorization_failed' => 'dashboards#authorization_failed'` – Pravin Mar 25 '11 at 06:39
  • so instead of `render_to_facebox(:template => dashboards_authorization_failed_path)` I could do `render_to_facebox(:partial => 'sessions/login_div')` and put the code in my _login_div.html.erb file? – Justin Meltzer Mar 25 '11 at 06:43
  • I think it should work that way too.. not sure.. I don't remember whether I tried or not.. Please update if it works :) – Pravin Mar 25 '11 at 06:49
0

WHat I would do in your case is on my page, I will usually have a "Login" or "Welcome, user!" at the top, depending if you're logged in or not.

Then by javascript, I can check if the "Login" text or div was missing when you try to vote. If it was there, i'd call the popup modal plugin or whatnot you have.

So basically, you just need a good js solution for this which is quite easy.

  1. user clicks vote
  2. sense that the user clicked the vote( in jquery this is done by .click() )
  3. Look for a sign in the page that user is not logged in
  4. if user is not logged in, popup mr. lightbox
  5. ???
  6. PROFIT
corroded
  • 21,406
  • 19
  • 83
  • 132