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

tt12580-github · 2017年03月20日 · 最后由 tt12580-github 回复于 2017年03月20日 · 1400 次阅读

我 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

dylin 回复

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

tt12580-github 回复

使用实例变量啊

dylin 回复

😂 不知道咋实现。。。

tt12580-github 回复
@message = "商品已上架!"

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

tt12580-github 回复

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

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

martin91 回复

5.0.0.1 版本的

tt12580-github 关闭了讨论。 03月20日 17:46
需要 登录 后方可回复, 如果你还没有账号请 注册新账号