Ruby on Rails Tuesday, February 19, 2013

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.


On Monday, February 18, 2013 4:15:28 PM UTC-5, Ruby-Forum.com User wrote:
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 via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/mmYAjbfRAv4J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment