[英]respond_with & Flash Notice
我的一個控制器中有以下內容:
def create
photo.user = current_user if current_user
flash[:notice] = "The photos was saved" if photo.save
respond_with photo, :location => photo_path(photo)
end
此代碼有一個例外。 如果我使用某種 AJAX 訪問此操作,然后訪問另一個頁面,則會顯示 flash 通知。 如何在響應時只顯示 flash 通知:html? 我想也許你可以將 a:notice => "my flash" 傳遞給respond_with
但沒有運氣。
您可以將條件設置為:
flash[:notice] = "The photos was saved" if photo.save && !request.xhr?
此外,如果有一天您決定在響應 AJAX 請求時使用生成的通知,您可以使用flash.discard
清除 flash。
使用如下:
respond_to do |format|
format.html { redirect_to photo_path(photo), :notice => 'The photos was saved') }
format.xml { render :xml => photo, :status => :created}
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.