Skip to content
Browse files

ajaxsubmit demo

  • Loading branch information...
1 parent 50bb2f9 commit aa5f42c9d8b4b759aa2286fbb366445f9c1f5e9a @bogdan committed Feb 5, 2012
View
11 app/controllers/password_resets_controller.rb
@@ -6,12 +6,13 @@ class PasswordResetsController < ApplicationController
def create
@user = User.find_by_email(params[:email])
- # This line sends an email to the user with instructions on how to reset their password (a url with a random token)
- @user.deliver_reset_password_instructions! if @user
+ if @user
+ @user.deliver_reset_password_instructions!
+ render :json => {:redirect => root_path}
+ else
+ render :json => {:errors => {:email => "not found"}}
+ end
- # Tell the user instructions have been sent whether or not email was found.
- # This is to not leak information to attackers about which emails exist in the system.
- redirect_to(root_path, :notice => 'Instructions have been sent to your email.')
end
# This is the reset password form.
View
12 app/controllers/user_sessions_controller.rb
@@ -6,14 +6,10 @@ def new
end
def create
- respond_to do |format|
- if @user = login(params[:email],params[:password],params[:remember])
- format.html { redirect_back_or_to(:users, :notice => 'Login successfull.') }
- format.xml { render :xml => @user, :status => :created, :location => @user }
- else
- format.html { flash.now[:alert] = "Login failed."; render :action => "new" }
- format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
- end
+ if @user = login(params[:email],params[:password],params[:remember])
+ render :json => {:redirect => users_path}
+ else
+ render :json => {:errors => {:base => "Login failed"}}
end
end
View
12 app/controllers/users_controller.rb
@@ -44,14 +44,10 @@ def edit
def create
@user = User.new(params[:user])
- respond_to do |format|
- if @user.save
- format.html { redirect_to(:users, :notice => 'Registration successfull. Check your email for activation instructions.') }
- format.xml { render :xml => @user, :status => :created, :location => @user }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
- end
+ if @user.save
+ render :json => {:redirect => root_path}
+ else
+ render :json => {:errors => @user.errors}
end
end
View
2 app/models/user.rb
@@ -6,6 +6,8 @@ class User < ActiveRecord::Base
authenticates_with_sorcery!
+ validates_format_of :email, :with => /^[A-Z0-9_\.%\+\-']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)$/i
+ validates_uniqueness_of :email
validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
validates_confirmation_of :password, :message => "should match confirmation", :if => :password
end
View
7 app/views/layouts/application.html.erb
@@ -3,7 +3,9 @@
<head>
<title>Collaboration</title>
<%= stylesheet_link_tag :all %>
- <%= javascript_include_tag :defaults %>
+ <%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" %>
+ <%= javascript_include_tag "ajaxsubmit" %>
+ <%= javascript_include_tag "application" %>
<%= csrf_meta_tag %>
</head>
<body>
@@ -15,9 +17,6 @@
<% else %>
<%= link_to "Register", new_user_path %> |
<%= link_to "Login", :login %> |
- <%= link_to 'Login from HTTP', login_from_http_basic_users_path %> |
- <%= link_to 'Login with Twitter', auth_at_provider_path(:provider => :twitter) %> |
- <%= link_to 'Login with Facebook', auth_at_provider_path(:provider => :facebook) %>
<% end %>
</div>
<% if current_user %>
View
6 app/views/user_sessions/_forgot_password_form.html.erb
@@ -1,6 +1,6 @@
-<%= form_tag password_resets_path, :method => :post do %>
- <div class="field">
+<%= form_tag password_resets_path, :method => :post, :class => "ajax-form" do %>
+ <div class="field" validate="email">
<%= label_tag :email %><br />
<%= text_field_tag :email %> <%= submit_tag "Reset my password!" %>
</div>
-<% end %>
+<% end %>
View
10 app/views/user_sessions/_form.html.erb
@@ -1,17 +1,13 @@
-<%= form_tag user_sessions_path, :method => :post do %>
- <div class="field">
+<%= form_tag user_sessions_path, :method => :post, :class => "ajax-form" do %>
+ <div class="field" validate="email">
<%= label_tag :email %><br />
<%= text_field_tag :email %>
</div>
- <div class="field">
+ <div class="field" validate="password">
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag "Login" %>
</div>
- <div>
- <%= label_tag "keep me logged in" %><br />
- <%= check_box_tag :remember %>
- </div>
<% end %>
View
2 app/views/user_sessions/new.html.erb
@@ -1,6 +1,4 @@
<h1>Login</h1>
-<p id="notice"><%= notice %></p>
-<p id="alert"><%= alert %></p>
<%= render 'form' %>
<h1>Forgot Password?</h1>
View
20 app/views/users/_form.html.erb
@@ -1,29 +1,19 @@
-<%= form_for(@user) do |f| %>
- <% if @user.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
+<%= form_for(@user, :html => {:class => "ajax-form"}) do |f| %>
- <ul>
- <% @user.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div class="field">
+ <div class="field" validate="email">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
- <div class="field">
+ <div class="field" validate="password">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
- <div class="field">
+ <div class="field" validate="password_confirmation">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
+
View
114 public/javascripts/ajaxsubmit.js
@@ -0,0 +1,114 @@
+/**
+* Ajax Submit v0.0.1
+* http://github.com/bogdan/ajaxsubmit
+*
+* Copyright 2011, Bogdan Gusiev
+* Released under the MIT License
+*/
+
+ (function($) {
+ var applyValidation, applyValidationMessage;
+ if (!($().jquery >= '1.6')) throw 'ajaxsubmit.js require jQuery >= 1.6.0';
+ $.errors = {
+ attribute: "validate",
+ activationClass: "error",
+ messageClass: "validation-message",
+ format: "<div class='validation'><div class='validation-message'></div><div class='arrow'></div></div>"
+ };
+ applyValidationMessage = function(div, message) {
+ var message_div;
+ if (!div.hasClass($.errors.activationClass)) {
+ div.addClass($.errors.activationClass);
+ message_div = div.find("." + $.errors.messageClass);
+ if (message_div.size() === 0) div.append($.errors.format);
+ message_div = div.find("." + $.errors.messageClass);
+ if (message_div.size() > 0) {
+ return message_div.html(message);
+ } else {
+ throw new Error("configuration error: $.errors.format must have elment with class " + $.errors.messageClass);
+ }
+ }
+ };
+ applyValidation = function(form, field, message) {
+ var div;
+ div = form.find("[" + $.errors.attribute + "~='" + field + "']");
+ if (div.size() === 0) {
+ div = $("<div " + $.errors.attribute + "='" + field + "'></div>");
+ form.prepend(div);
+ }
+ return applyValidationMessage(div, message);
+ };
+ $.fn.applyErrors = function(errors) {
+ var form;
+ form = $(this);
+ $(this).clearErrors();
+ if ($.type(errors) === "object") {
+ errors = $.map(errors, function(v, k) {
+ return [[k, v]];
+ });
+ }
+ return $(errors).each(function(key, error) {
+ var field, message;
+ field = error[0];
+ message = error[1];
+ if ($.isArray(message)) message = message[0];
+ return applyValidation(form, field, message);
+ });
+ };
+ return $.fn.clearErrors = function() {
+ var validators;
+ validators = $(this).find("[" + $.errors.attribute + "]");
+ return validators.removeClass($.errors.activationClass);
+ };
+ })(jQuery);
+
+ (function($) {
+ var ajaxFormErrorHandler, ajaxFormSuccessHandler;
+ $.fn.ajaxSubmit = function(options) {
+ var $form, callback, error_callback, method, url;
+ if (options == null) options = {};
+ $form = $(this);
+ $form.clearErrors();
+ if (typeof options === "function") options.success = options;
+ if (options.redirect && !options.success) {
+ options.success = function() {
+ return window.location = options.redirect;
+ };
+ }
+ callback = options.success;
+ error_callback = options.error;
+ method = $form.attr("method") || "GET";
+ url = $form.attr("action");
+ return $.ajax({
+ type: method.toUpperCase(),
+ url: url,
+ data: $form.serialize(),
+ success: function(data) {
+ return ajaxFormSuccessHandler($form, data, callback, error_callback);
+ },
+ error: function(xhr, status, str) {
+ return ajaxFormErrorHandler($form);
+ }
+ });
+ };
+ ajaxFormSuccessHandler = function($form, data, callback, error_callback) {
+ if ($.isEmptyObject(data && data.errors)) {
+ if (typeof callback === "function") {
+ return callback.call($form[0], data);
+ } else if (data.redirect) {
+ return window.location = data.redirect;
+ }
+ } else {
+ if (typeof error_callback === "function") error_callback.call($form, data);
+ return $form.applyErrors(data.errors);
+ }
+ };
+ ajaxFormErrorHandler = function($form) {};
+ return $.fn.ajaxForm = function(options) {
+ if (options == null) options = {};
+ return $(this).bind("submit", function(event) {
+ event.preventDefault();
+ return $(this).ajaxSubmit(options);
+ });
+ };
+ })(jQuery);
View
3 public/javascripts/application.js
@@ -1,2 +1,5 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
+$(function () {
+ $('.ajax-form').ajaxForm();
+});

0 comments on commit aa5f42c

Please sign in to comment.
Something went wrong with that request. Please try again.