Created
August 17, 2011 19:31
-
-
Save gertig/1152393 to your computer and use it in GitHub Desktop.
Devise Ajax Signup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#registrations_controller.rb | |
class RegistrationsController < Devise::RegistrationsController | |
def create | |
params[:user][:password_confirmation] = params[:user][:password] | |
super | |
end | |
end | |
<!-- Inside any View ---> | |
<%= form_for(:user, :url => "#", :html => { :id => "ajax_signup"}) do |f| %> | |
<p><%= f.email_field :email, :placeholder => "Email" %></p> | |
<p><%= f.password_field :password, :placeholder => "Password" %></p> | |
<p><%= f.submit "Sign up" %></p> | |
<% end %> | |
// application.js | |
// Place your application-specific JavaScript functions and classes here | |
// This file is automatically included by javascript_include_tag :defaults | |
$.fn.serializeObject = function() { | |
var values = {} | |
$("form input, form select, form textarea").each( function(){ | |
values[this.name] = $(this).val(); | |
}); | |
return values; | |
} | |
$(function(){ | |
$("form#ajax_signup").submit(function(e){ | |
e.preventDefault(); //This prevents the form from submitting normally | |
var user_info = $(this).serializeObject(); | |
console.log("About to post to /users: " + JSON.stringify(user_info)); | |
$.ajax({ | |
type: "POST", | |
url: "http://localhost:3000/users", | |
data: user_info, | |
success: function(json){ | |
console.log("The Devise Response: " + JSON.stringify(json)); | |
//alert("The Devise Response: " + JSON.stringify(json)); | |
}, | |
dataType: "json" | |
}); | |
}); | |
}); |
@gertig one question.. Will normal Sign Up form generated by Devise will work after this? Can we have both?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to add:
to the top of the RegistrationsController override, (as of Devise 2.2.0, anyway).