Wire up adoption form

This commit is contained in:
Erik Michaels-Ober 2011-03-08 00:44:27 -08:00
parent 80994fa76b
commit 200e683f91
4 changed files with 73 additions and 32 deletions

View File

@ -0,0 +1,12 @@
class HydrantsController < ApplicationController
respond_to :json
def update
@hydrant = Hydrant.find(params[:id])
if @hydrant.update_attributes(params[:hydrant])
respond_with @hydrant
else
render(:json => {"errors" => @hydrant.errors})
end
end
end

View File

@ -18,11 +18,11 @@
position: google.maps.ControlPosition.BOTTOM_CENTER position: google.maps.ControlPosition.BOTTOM_CENTER
}, },
zoom: <%= @zoom %>, zoom: <%= @zoom %>,
zoomControl: false,
center: latlng, center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP mapTypeId: google.maps.MapTypeId.ROADMAP
}; };
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
comboFormString = '<%= render(:partial => "combo_form").gsub("\n", "") %>';
var openInfoWindow; var openInfoWindow;
function addMarker(point, color, contentString) { function addMarker(point, color, contentString) {
var image = new google.maps.MarkerImage(color, var image = new google.maps.MarkerImage(color,
@ -42,13 +42,36 @@
shadow: shadow shadow: shadow
}); });
var infoWindow = new google.maps.InfoWindow({ var infoWindow = new google.maps.InfoWindow({
content: contentString content: contentString,
maxWidth: 350
}); });
google.maps.event.addListener(marker, 'click', function() { google.maps.event.addListener(marker, 'click', function() {
if(openInfoWindow != undefined) { if(openInfoWindow) {
openInfoWindow.close(); openInfoWindow.close();
} }
infoWindow.open(map, marker); infoWindow.open(map, marker);
$('.adoption_form').submit(function() {
$.post('<%= hydrant_path :format => "json" %>', {
'id': $('#hydrant_id').val(),
'commit': $('#adoption_form_submit').val(),
'utf8': '✓',
'authenticity_token': $('input[name="authenticity_token"]').val(),
'_method': 'put',
'hydrant': {
'user_id': $('#hydrant_user_id').val() || <%= user_signed_in? ? current_user.id : false %>,
'name': $('#hydrant_name').val()
}
}, function(data) {
image = new google.maps.MarkerImage('/images/markers/green.png',
new google.maps.Size(27.0, 37.0),
new google.maps.Point(0, 0),
new google.maps.Point(13.0, 18.0)
);
marker.setIcon(image);
marker.setAnimation(google.maps.Animation.BOUNCE);
});
return false;
});
$('.combo_form').submit(function() { $('.combo_form').submit(function() {
var errors = [] var errors = []
if(!/[\w\.%\+\-]+@[\w\-]+\.+[\w]{2,}/.test($('#user_email').val())) { if(!/[\w\.%\+\-]+@[\w\-]+\.+[\w]{2,}/.test($('#user_email').val())) {
@ -80,17 +103,17 @@
errors[0].focus(); errors[0].focus();
} else { } else {
$.post('<%= user_registration_path :format => "json" %>', { $.post('<%= user_registration_path :format => "json" %>', {
'remote' : true, 'commit': $('#user_new_submit').val(),
'commit' : $('#user_new_submit').val(), 'utf8': '✓',
'utf8' : '✓', 'authenticity_token': $('input[name="authenticity_token"]').val(),
'user' : { 'user': {
'email' : $('#user_email').val(), 'email': $('#user_email').val(),
'name' : $('#user_name').val(), 'name': $('#user_name').val(),
'organization' : $('#user_organization').val(), 'organization': $('#user_organization').val(),
'voice_number' : $('#user_voice_number').val(), 'voice_number': $('#user_voice_number').val(),
'sms_number' : $('#user_sms_number').val(), 'sms_number': $('#user_sms_number').val(),
'password' : $('#user_password_confirmation').val(), 'password': $('#user_password_confirmation').val(),
'password_confirmation' : $('#user_password_confirmation').val() 'password_confirmation': $('#user_password_confirmation').val()
} }
}, function(data) { }, function(data) {
if(data.errors) { if(data.errors) {
@ -127,7 +150,9 @@
errors[0].focus(); errors[0].focus();
} else { } else {
$('.combo_form').slideUp(); $('.combo_form').slideUp();
$('.adoption_form').slideDown(); $('.adoption_form').slideDown(function() {
$('#hydrant_user_id').val(data.user.id);
});
} }
}); });
} }
@ -144,12 +169,13 @@
errors[0].focus(); errors[0].focus();
} else { } else {
$.post('<%= user_session_path :format => "json" %>', { $.post('<%= user_session_path :format => "json" %>', {
'remote' : true, 'commit': $('#user_existing_submit').val(),
'commit' : $('#user_existing_submit').val(), 'utf8': '✓',
'utf8' : '✓', 'authenticity_token': $('input[name="authenticity_token"]').val(),
'user' : { 'user': {
'email' : $('#user_email').val(), 'email': $('#user_email').val(),
'password' : $('#user_password').val() 'password': $('#user_password').val(),
'remember_me': $('#user_remember_me').val()
} }
}, function(data) { }, function(data) {
if(data.errors) { if(data.errors) {
@ -158,7 +184,9 @@
$('#user_password').addClass('error', 500); $('#user_password').addClass('error', 500);
} else { } else {
$('.combo_form').slideUp(); $('.combo_form').slideUp();
$('.adoption_form').slideDown(); $('.adoption_form').slideDown(function() {
$('#hydrant_user_id').val(data.user.id);
});
} }
}); });
} }
@ -167,11 +195,11 @@
errors[0].focus(); errors[0].focus();
} else { } else {
$.post('<%= user_password_path :format => "json" %>', { $.post('<%= user_password_path :format => "json" %>', {
'remote' : true, 'commit': $('#user_forgot_password_submit').val(),
'commit' : $('#user_forgot_password_submit').val(), 'utf8': '✓',
'utf8' : '✓', 'authenticity_token': $('input[name="authenticity_token"]').val(),
'user' : { 'user': {
'email' : $('#user_email').val() 'email': $('#user_email').val()
} }
}, function(data) { }, function(data) {
if(data.errors) { if(data.errors) {
@ -199,7 +227,7 @@
<% if user_signed_in? %> <% if user_signed_in? %>
contentString = '<h2>Adopt this Hydrant</h2><%= render(:partial => "adoption_form", :locals => {:hydrant => hydrant}).gsub("\n", "") %>' contentString = '<h2>Adopt this Hydrant</h2><%= render(:partial => "adoption_form", :locals => {:hydrant => hydrant}).gsub("\n", "") %>'
<% else %> <% else %>
contentString = '<h2>Adopt this Hydrant</h2><%= render(:partial => "combo_form").gsub("\n", "") + render(:partial => "adoption_form", :locals => {:hydrant => hydrant}).gsub("\n", "") %>' contentString = '<h2>Adopt this Hydrant</h2>' + comboFormString + '<%= render(:partial => "adoption_form", :locals => {:hydrant => hydrant}).gsub("\n", "") %>'
<% end %> <% end %>
<% end %> <% end %>
addMarker(point, color, contentString); addMarker(point, color, contentString);

View File

@ -1,10 +1,11 @@
<% style = user_signed_in? ? {} : {:style => "display: none;"}%> <% style = user_signed_in? ? {} : {:style => "display: none;"}%>
<%= form_for hydrant, :html => {:class => "adoption_form"}.merge(style) do |f| %> <%= form_for hydrant, :remote => true, :html => {:class => "adoption_form"}.merge(style) do |f| %>
<div id="adoption_fields"> <div id="adoption_fields">
<%= f.hidden_field "id" %>
<%= f.hidden_field "user_id", :value => (user_signed_in? ? current_user.id : nil) %> <%= f.hidden_field "user_id", :value => (user_signed_in? ? current_user.id : nil) %>
<%= f.label "name", "Name your hydrant", :id => "hydrant_name_label" %> <%= f.label "name", "Give your hydrant a name", :id => "hydrant_name_label" %>
<%= f.text_field "name", :tabindex => 1 %> <%= f.text_field "name", :tabindex => 1 %>
<%= f.submit "Adopt!", :tabindex => 2, :id => "user_new_submit" %> <%= f.submit "Adopt!", :tabindex => 2, :id => "adoption_form_submit" %>
<p>By adopting this hydrant, you agree to the <%= link_to "Terms of Service", "#", :tabindex => 3 %>.</p> <p>By adopting this hydrant, you agree to the <%= link_to "Terms of Service", "#", :tabindex => 3 %>.</p>
</div> </div>
<% end %> <% end %>

View File

@ -1,4 +1,4 @@
<%= form_for :user, :html => {:class => "combo_form"} do |f| %> <%= form_for :user, :remote => true, :html => {:class => "combo_form"} do |f| %>
<div id="common_fields"> <div id="common_fields">
<%= f.label "email", "Email address", :id => "user_email_label" %> <%= f.label "email", "Email address", :id => "user_email_label" %>
<%= f.text_field "email", :tabindex => 1 %> <%= f.text_field "email", :tabindex => 1 %>