繁体   English   中英

如何使用Ajax和jQuery在Rails 3中显示Flash错误消息?

[英]How show flash error messages in Rails 3 using Ajax and jQuery?

我的控制器是:

def create
  @group = Group.new(params[:group])

  @group.company=current_user.company
  respond_to do |format|
    if @group.save
      format.html { redirect_to(@group, :notice => 'Group was successfully created.') }
      format.js
    else
      flash.now[:error]=@group.errors.full_messages
      format.html { render :nothing => true }
      format.js
    end
  end
end

create.js.erb是:

$('<%= escape_javascript(render(:partial => @group))%>').appendTo('#groups');
$("#new_group")[0].reset();
$(".flashnotice").html("<%= escape_javascript(flash[:notice]) %>");  
$(".flashnotice").show(300);

您必须在create.js.erb文件中添加这两行。 .flashnotice替换为Flash HTML元素的选择器; 例如,您的Flash <div>的类名。

我认为您只需要在erb文件中的某处说<%= flash[:notice] %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM