2011-04-19 52 views
1

我的控制器:如何在使用Ajax和jQuery的Rails 3中顯示Flash錯誤消息?

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][email protected]_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(); 

回答

-4

我想你只需要說<%= flash[:notice] %>某處你的ERB文件。

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

您必須將這兩行添加到create.js.erb文件中。用您的Flash HTML元素的選擇器替換.flashnotice;例如您的閃存的類名稱<div>

+1

我想這種方法在顯示通知後需要'<%flash.discard%>',以防止通知出現在後續頁面上。 – 2013-08-22 18:55:27

相關問題