Permalink
Browse files

add validation to /posts/; adjust error message on js side

  • Loading branch information...
1 parent 0559c3c commit a0787a8917402a5884109fabe133231bf8b5350b @neolee committed May 18, 2012
Showing with 78 additions and 16 deletions.
  1. +20 −13 main.rb
  2. +41 −0 public/js/post.js
  3. +2 −2 public/js/user.js
  4. +15 −1 views/_post_form.haml
View
33 main.rb
@@ -87,11 +87,7 @@ class Main < Base
if !user.update(data)
error_msgs = Array.new
- user.errors.each do |e|
- logger.info(e[0])
- error_msgs << e[0]
- end
- logger.info(error_msgs)
+ user.errors.each {|e| error_msgs << e[0]}
error 400, error_msgs.join(';')
end
end
@@ -101,6 +97,11 @@ class Main < Base
haml :list
end
+ get '/posts/:id' do
+ @post = Post.get(params[:id])
+ haml :post
+ end
+
get '/posts/new/?' do
haml :new
end
@@ -112,20 +113,26 @@ class Main < Base
:created_at => Time.now
)
post.user_id = session[:user_id]
- post.save
- redirect '/posts/'
- end
+ if !post.save
+ error_msgs = Array.new
+ post.errors.each {|e| error_msgs << e[0]}
+ error 400, error_msgs.join(';')
+ end
- get '/posts/:id' do
- @post = Post.get(params[:id])
- haml :post
+ redirect '/posts/'
end
patch '/posts/:id' do
post = Post.get(params[:id])
- if post.user_id == session[:user_id].to_i
- post.update(:title => params[:title], :body => params[:body], :edited_at => Time.now)
+ if post.user_id != session[:user_id].to_i
+ error 401
+ end
+
+ if !post.update(:title => params[:title], :body => params[:body], :edited_at => Time.now)
+ error_msgs = Array.new
+ post.errors.each {|e| error_msgs << e[0]}
+ error 400, error_msgs.join(';')
end
end
View
@@ -0,0 +1,41 @@
+$('##{commit_id}').click(function(event) {
+ $('##{alert_id}').removeClass('alert-success alert-error').addClass('alert-info');
+ $('##{alert_id}').show().text('Saving...');
+
+ $.ajax({
+ type: $('##{form_id}').attr('method'),
+ url: $('##{form_id}').attr('action'),
+ data: $('##{form_id}').serialize(),
+ success: function(data) {
+ $('##{alert_id}').removeClass('alert-info').addClass('alert-success');
+ $('##{alert_id}').text('Saved').fadeOut('slow');
+ if ('#{locals[:mode]}' == 'new') {
+ location = '/';
+ }
+ else {
+ location.reload();
+ }
+ },
+ error: function(data) {
+ switch(data.status)
+ {
+ case 401:
+ errorMsg = 'You are not authorized to do this.'
+ break;
+ case 304:
+ errorMsg = 'Nothing changed.'
+ break;
+ case 400:
+ errorMsg = 'Cannot save changes [Error: ' + data.responseText + '].'
+ break;
+ default:
+ errorMsg = 'Failed [Error code: ' + data.status + '].'
+ }
+
+ $('##{alert_id}').removeClass('alert-info').addClass('alert-error');
+ $('##{alert_id}').text(errorMsg);
+ },
+ });
+
+ return false;
+});
View
@@ -23,10 +23,10 @@ $('#formUser').submit(function(event) {
errorMsg = 'Nothing changed.'
break;
case 400:
- errorMsg = 'Cannot save changes. (' + data.responseText + ')'
+ errorMsg = 'Cannot save changes [Error: ' + data.responseText + '].'
break;
default:
- errorMsg = 'Failed. (Error code: ' + data.status + ')'
+ errorMsg = 'Failed [Error code: ' + data.status + '].'
}
$('#alert').removeClass('alert-info').addClass('alert-error');
View
@@ -51,7 +51,21 @@
}
},
error: function(data) {
- errorMsg = 'Failed. (Error code: ' + data.status + ')'
+ switch(data.status)
+ {
+ case 401:
+ errorMsg = 'You are not authorized to do this.'
+ break;
+ case 304:
+ errorMsg = 'Nothing changed.'
+ break;
+ case 400:
+ errorMsg = 'Cannot save changes [Error: ' + data.responseText + '].'
+ break;
+ default:
+ errorMsg = 'Failed [Error code: ' + data.status + '].'
+ }
+
$('##{alert_id}').removeClass('alert-info').addClass('alert-error');
$('##{alert_id}').text(errorMsg);
},

0 comments on commit a0787a8

Please sign in to comment.