私のプロジェクトから抽出:
_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
update.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