【问题标题】:Flash message with remote actions with Rails3使用 Rails3 进行远程操作的 Flash 消息
【发布时间】:2010-09-01 17:03:15
【问题描述】:

我有一个愚蠢的问题。 我被困住了,我不知道如何解决它:)

我有一些基本操作,例如创建和销毁 cmets。我添加了 remote => true 属性。

我的动作是使用 create.js.haml 和 destroy.js.haml 呈现的。

$(".comments").prepend("#{escape_javascript(render(@comment))}");
$("#comments_box .form #new_comment").get(0).reset();

在我的控制器中,我有一个

flash[:notice] = 'Comment created'

我想将 Flash 消息更新到布局中...... 在我的布局中,我有这个:

 - flash.each do |name, msg|
        = content_tag :div, msg, :id => "flash_#{name}"

从 jQuery(视图)创建或更新 flash 消息的更好方法是什么?

谢谢:)

【问题讨论】:

    标签: jquery ruby-on-rails-3


    【解决方案1】:

    我这样解决了我的问题:

    在我的布局中我添加了:

    #flash_messages
      = render :partial => 'layouts/flash', :locals => { :flash => flash }
    

    flash 部分包含:

    - flash.each do |name, msg|
      = content_tag :div, msg, :id => "flash_#{name}"
    - if defined?(flush_flash) and flush_flash == true
      - flash.clear
    

    在我的 js.haml 中

    $("#flash_messages").html("#{escape_javascript(render(:partial => 'layouts/flash', :locals => {:flash => flash, :flush_flash => true}))}");
    

    谢谢 ;)

    【讨论】:

    • 您不应该调用部分闪存,您可能会发生名称冲突。如果您想避免头痛,请将部分重命名为 _flash_messages.html.erb
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多