新手问题 请教使用 remote 后 flash 怎么显示

tt12580-github · March 20, 2017 · Last by tt12580-github replied at March 20, 2017 · 1400 hits

我 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 %>");

flash 是在 302,也就是 redirect_to 的时候使用 http://api.rubyonrails.org/classes/ActionDispatch/Flash.html

Reply to dylin

哦,那这种应该怎么显示提示

使用实例变量啊

Reply to dylin

😂 不知道咋实现。。。

@message = "商品已上架!"

然后前面 view 就使用这个 实例变量就可以了

用的是哪个版本 rails 啊,按照文档的话,flash.now 应该是满足需求的

Sets a flash that will not be available to the next action, only to the current.

Reply to martin91

5.0.0.1 版本的

tt12580-github closed this topic. 20 Mar 17:46
You need to Sign in before reply, if you don't have an account, please Sign up first.