Javascript 在Ajax警报中显示Rails错误消息

Javascript 在Ajax警报中显示Rails错误消息,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,使用Rails 3.2。我想知道如何将Rails中的错误推送到Ajax警报中显示。这是我的密码: # posts_controller.rb def update @post = Post.find(params[:id]) if @post.update_attributes(params[:name] => params[:value]) render :nothing => true else render :text => @post.er

使用Rails 3.2。我想知道如何将Rails中的错误推送到Ajax警报中显示。这是我的密码:

# posts_controller.rb
def update
  @post = Post.find(params[:id])

  if @post.update_attributes(params[:name] => params[:value])
    render :nothing => true
  else
    render :text => @post.errors.full_messages[0], :status => :unprocessable_entity
  end
end

# _form.html.erb
$("#status_buttons button").click(function() {
  $.ajax({
    type: 'POST',
    url: "<%= post_path(@trip) %>",
    data: { _method: 'PUT', name: this.name, value: this.value },
    error: function() {
      alert(" *****************Rails error message here**********************");
    }
  });
});
#posts_controller.rb
def更新
@post=post.find(参数[:id])
如果@post.update_属性(params[:name]=>params[:value])
render:nothing=>true
其他的
render:text=>@post.errors.full_messages[0],:status=>:unprocessable_entity
结束
结束
#_form.html.erb
$(“#状态按钮”)。单击(函数(){
$.ajax({
键入:“POST”,
url:“”,
数据:{u方法:'PUT',名称:this.name,值:this.value},
错误:函数(){
警报(“***************************此处为Rails错误消息****************************”);
}
});
});
常规验证错误消息应出现在
警报()中。有什么想法吗


谢谢。

确定的一种方法是向回调函数传递至少3个参数,并检查控制台(或警报)中传递错误的任何参数

error: function(a,b,c) {
  alert('a contains ' + a);
  alert('b contains ' + b);
  alert('c contains ' + c);
}
其中一个应该包含错误消息:)祝你好运

error: function( jqXHR, textStatus, errorThrown ) { ...