2017-01-28 4 views
0

このGistに従って、クラッシュレールアプリhttps://gist.github.com/suryart/7418454のブートストラップアラートを表示しようとしています。その内容は、コードを持っている:Railsはヘルパーを使用してブートストラップアラートを表示します

application_helper.rb:

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> 

問題は、私は、任意のを見ていないですですApplicationHelperモジュールメッセージが表示されます。

私は要旨はそのnilを返すように見せているかもしれないので、多分私は私がHTMLを返すために、私のヘルパーにこのような何かをしたように、.eachイテレータの内容を返す必要があると考え:

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 

しかし、あなたはprint文の出力を見ると、それはページのHTMLを入力して、私を見せて、実際に私が必要htmlです端が:

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

私は仕事にこれを取得するにはどうすればよいこととなるようページ全体ではなく最後の部分だけを返しますか?

答えて

1

非出力コードブロック<% flash_messages %>(Not <%=)を要点の元のコードとともに使用する必要があります。

concatはテンプレートコンテキストにhtmlマークアップを挿入する責任があり、ヘルパーの戻り値は意味を持ちません。

関連する問題