Forum: Ruby on Rails Login with Ajax

Posted by Ryo Saeba (sirjay)
on 2013-02-18 22:15
I'm trying to create an Login Form, that is retrieved via Ajax and
creates a Session via Ajax.

In my Login Form I typed
<%= form_tag sessions_path, :remote => true do %>

Session Controller
  def new
    respond_to do |format|
      format.js
    end
  end

And in my session vier directory I created a js.erb
$(".login").update("<%= escape_javascript(render('sessions/new')) %>");

the login class spans the whole content of my login form, but trying to
open the login form only throws me a completely blank page :/

Is my jquery function not allright, or am I missing out on something?
I appreciate any kind of help!
Posted by javinto (Guest)
on 2013-02-18 22:36
(Received via mailing list)
The default method for a form_tag is a POST.

But in your controller you are only showing a the #new method (GET). 
Should
it not be #create?


Op maandag 18 februari 2013 22:15:28 UTC+1 schreef Ruby-Forum.com User 
het
volgende:
Posted by Ryo Saeba (sirjay)
on 2013-02-19 13:37
you are right, but I'm not only trying to execute the login post via 
ajax, but also retrieve the login form via ajax first. Once I accomplish 
this, I can go to the next step.

Right now I want to open the login form in a small popup box, that is 
retrieved by ajax :/

What is missing in my Source Code?
Posted by David Hersey (Guest)
on 2013-02-19 21:31
(Received via mailing list)
I just ran into this last week, and solved it as follows:

1) Render the login form in a hidden div when the containing page is
rendered
2) Unhide the login div when the user clicks the login link (hook this
event after the main page is loaded -- I used jquery's delegate method 
as
follows:

$('#headerwrapper').delegate('a', 'click', function(event) {
var id=$(event.target).attr("id")
if (id == 'login') {
 event.preventDefault();
                 $('.login_wrapper').removeClass('hidden');
}
});

3) The login div should be rendered using a rails form or form tag 
helper
with remote: true

This avoids an extra trip to the controller just to GET html for a form.
Since my login form has no dynamic data on it that is not available when
the containing page is loaded, this works.

Note: if you are using Devise then you're going to have to revise most 
of
the controller methods to operate via Ajax/UJS.  This is not as hard as 
it
sounds, but reply if you need help.
Posted by Ryo Saeba (sirjay)
on 2013-02-19 22:52
Thanks for your help!

But unfortunately I can't quite follow you.

1. You mean I should be redirected to my login page, with the 
login-content being invisivle?

Actually I want some kind of Login Box, that pops up anywhere, since 
several features in my application are filtered by a "require login" 
helper-method.

I bothered to find a way redirecting to the current page after a login, 
and using ajax I wouldn't have to deal with that. So I need a Solution 
that replaces any "redirec to login_form"-method with an event, that 
pops up a login-box!

So far my Login (creating the Session) is working fine. My Login Form 
looks like this
<%= form_tag sessions_path, :remote => true do %>

I also have put this login form in a partial, that is rendered by 
jQuery, whenever I click on "login" which doesn't have a path-helper but 
only a class.

So what I'm missing out is, that this only works, when I click on 
"login", but once a login is required due to a "require_login" method, I 
get redirected to my Loginform Page.

This is my Helper Method

  def require_login
    unless user_signed_in?
      redirect_to login_path,
        alert: "You need to login first"
    end
  end

How do I change that so I don't get routed to some other page, but a 
hidden div gets revealed?
render :partial => "sessions/login" only renders a compoletely white 
page with no styles.

This is my jQuery function

$(document).ready ->
  $(".button_login").click ->
    $(".login").show()

It only responds to this very login button, but not the redirecting 
method...

I hope you can follow me and help me out...thanks in advance!!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.