2017-01-28 65 views
0

我正在尝试关注此要点,以显示Crud Rails应用程序https://gist.github.com/suryart/7418454的Bootstrap警报。其内容有代码:Rails使用助手显示Bootstrap警报

application_helper.rb:

模块ApplicationHelper

def bootstrap_class_for flash_type 
    { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s 
    end 

    def flash_messages(opts = {}) 
    flash.each do |msg_type, message| 
     concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do 
       concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' }) 
       concat message 
      end) 
    end 
    nil 
    end 

application.html.erb

<body> 
    <div class="container"> 

     <%= flash_messages %> 

     <%= yield %> 
    </div><!-- /container --> 
</body> 

问题是我没有看到任何的显示消息。

我想,也许因为要旨显示返回零,也许我需要返回。每个迭代的内容,所以我做了这样的事情在我的帮助返回的HTML:

def flash_messages 
    @flash_msg = "" 
    flash.each do |msg_type, message| 
     @flash_msg = concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do 
        concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' }) 
        concat message 
        end) 
     puts "@flash_msg: #{@flash_msg}" 
    end 
    @flash_msg 
    end 

但是,当你查看打印语句的输出它显示我的页面中输入HTML,结束这实际上是我需要的HTML:

<div class="alert alert-info fade in"><button class="close" data-dismiss="alert">x</button>Signed in successfully.</div> 

我如何获得这这样的工作,它只返回最后一部分而不是整个页面?

回答

1

您应该使用非输出代码块:<% flash_messages %>(不是<%=)以及您的要点的原始代码。

concat负责在模板上下文中注入html标记,帮助器的返回值没有意义。