[refs #616] add error messages to ajax

This commit is contained in:
Alexander Machehin 2012-09-24 23:34:14 +06:00
parent f4bbeaaa46
commit 2c6d4bae9e
5 changed files with 23 additions and 7 deletions

View File

@ -13,6 +13,7 @@ $(document).ready(function() {
$('form.edit_comment').live('submit', function() {
var form = $(this);
form.parent().find('.flash').remove();
$.ajax({
type: 'POST',
url: form.attr("action"),
@ -23,7 +24,7 @@ $(document).ready(function() {
$('.buttons a.edit_comment#'+cancel_button.attr('id')).parent().parent().find('.cm-s-default.md_and_cm').html(data).find('code').each(function (code) { CodeMirrorRun(this); })
},
error: function(data){
alert('error'); // TODO remove
form.before(data.responseText);
}
});
return false;

View File

@ -224,6 +224,7 @@ $(document).ready(function() {
$('.edit_form.issue').live('submit', function() {
var form = $(this);
form.parent().find('.flash').remove();
$.ajax({
type: 'POST',
url: form.attr("action"),
@ -235,7 +236,7 @@ $(document).ready(function() {
$('.fulltext.view.issue_body').html(data).find('code').each(function (code) { CodeMirrorRun(this); })
},
error: function(data){
alert('error'); // TODO remove
form.before(data.responseText);
}
});
return false;

View File

@ -23,8 +23,12 @@ class Projects::CommentsController < Projects::BaseController
end
def update
status = @comment.update_attributes(params[:comment]) ? 200 : 500
render :inline => view_context.markdown(@comment.body), :status => status
status, message = if @comment.update_attributes(params[:comment])
[200, view_context.markdown(@comment.body)]
else
[400, view_context.local_alert(@comment.errors.full_messages.join('. '))]
end
render :inline => message, :status => status
end
def destroy

View File

@ -51,10 +51,14 @@ class Projects::IssuesController < Projects::BaseController
if params[:issue] && status = params[:issue][:status]
@issue.set_close(current_user) if status == 'closed'
@issue.set_open if status == 'open'
render :partial => 'status', :status => (@issue.save ? 200 : 500)
render :partial => 'status', :status => (@issue.save ? 200 : 400)
elsif params[:issue]
status = @issue.update_attributes(params[:issue]) ? 200 : 500
render :inline => view_context.markdown(@issue.body), :status => status
status, message = if @issue.update_attributes(params[:issue])
[200, view_context.markdown(@issue.body)]
else
[400, view_context.local_alert(@issue.errors.full_messages.join('. '))]
end
render :inline => message, :status => status
else
render :nothing => true, :status => 200
end

View File

@ -48,4 +48,10 @@ module ApplicationHelper
end
@redcarpet.render(text).html_safe
end
def local_alert(text, type = 'error')
html = "<div class='flash'><div class='alert #{type}'> #{text}"
html << link_to('×', '#', :class => 'close close-alert', 'data-dismiss' => 'alert')
html << '</div></div>'
end
end