我 google 了好多都没成功
# application.html.erb
<div id='notice'>
<%= render :partial => 'layouts/flash' , :locals => { :flash => flash } %>
</div>
# layouts/_flash.html.erb
<% flash.each do |name, msg| %>
<div class="alert-message info">
<a class="close dismiss" href="#">x</a>
<p><%= msg %></p>
</div>
<% end %>
# controller
def active
respond_to do |format|
if @groupon_item.active
@groupon_item.update_attributes(active: false)
format.js {
flash.now[:success] = "商品已下架"
render action: 'active', status: :active, location: @groupon_item
}
else
@groupon_item.update_attributes(active: true)
format.js {
flash.now[:success] = "商品已上架!"
render action: 'active', status: :active, location: @groupon_item
}
end
end
end
# view
<%= button_to active_text, active_commission_groupon_path(groupon_item), remote: true, method: :patch, data: { confirm: '确定此操作?' }, class: "btn btn-white" %>
# active.js.erb
$("#notice").html("<%= escape_javascript(render :partial => 'layouts/flash' , :locals => { :flash => flash }).html_safe %>");