如何使用Ajax和jQuery在Rails3中显示flash错误消息?

如何使用Ajax和jQuery在Rails3中显示flash错误消息?,ajax,ruby-on-rails-3,jquery,Ajax,Ruby On Rails 3,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

我的控制器是:

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();
$('@group))%>')。附加到('#groups');
$(“#新#组”)[0]。重置();

我想你只需要在你的erb文件的某个地方说

$(“.flashnotice”).html(“”;
$(".flashnotice").html("<%= escape_javascript(flash[:notice]) %>");  
$(".flashnotice").show(300);
$(“.flashnotice”).show(300);

您必须在
create.js.erb
文件中添加这两行。将
.flashnotice
替换为flashhtml元素的选择器;e、 例如,flash的类名

我认为,通过这种方法,您希望在显示通知后
,避免通知出现在后续页面上。