Skip to content

Instantly share code, notes, and snippets.

@gertig
Created August 17, 2011 19:31
Show Gist options
  • Save gertig/1152394 to your computer and use it in GitHub Desktop.
Save gertig/1152394 to your computer and use it in GitHub Desktop.
Devise Ajax Signup
#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"
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment