从我的项目中提取:
_flash_notices.html.erb
<div class="noticesWrapper">
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>
<%= msg %>
</div>
<% end %>
</div>
!请参阅 noticesWrapper 元素,因为您需要在其中呈现通知。
application_controller.rb
after_filter :add_flash_to_header
def add_flash_to_header
# only run this in case it's an Ajax request.
return unless request.xhr?
# add different flashes to header
response.headers['X-Flash-Error'] = flash[:error] unless flash[:error].blank?
response.headers['X-Flash-Warning'] = flash[:warning] unless flash[:warning].blank?
response.headers['X-Flash-Notice'] = flash[:notice] unless flash[:notice].blank?
response.headers['X-Flash-Message'] = flash[:message] unless flash[:message].blank?
# make sure flash does not appear on the next page
flash.discard
end
更新.js.erb
$('.noticesWrapper').html("<%= j(render partial: 'layouts/flash_notices') %>");
your_controller.rb
def update
@project = Project.find(params[:id])
if @project.update_attributes(params[:project])
flash.now[:notice] = "Project updated"
else
flash.now[:error] = "Project not updated"
end
end
end