1

私はこの小さなヘルパーメソッドを書きました:

def alert_color(name)
    if name == 'notice'
        return 'alert alert-dismissable alert-success'
    end
end

私のアプリケーションのレイアウトでは、次のように書きました。

<% flash.each do |name, msg| %>
    <div class=<%= alert_color(name) %>>
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <strong><%= name %></strong><%= msg %>
    </div>
<% end %> 

私の最初の問題は、名前が helper_method に正しく渡されないため、何とか機能しないことです!

そして2番目の問題は、私が試したことです:

alert_color('notice')

そしてそれはこれを返しました:

<div class="alert" alert-success="" alert-dismissable="">

この動作を変更する方法が本当にわかりません!

そして、私はこの方法でフラッシュメッセージを生成しています:

notice: 'User was successfully updated.'
4

2 に答える 2

2
<div class="<%= alert_color(name) %>">

さらに、ヘルパーで「成功」以外のケースもコーディングする必要があります。

def alert_color(name)
  color = name == 'notice' ? 'success' : 'alert'
  "alert alert-dismissable alert-#{color}"
end
于 2013-10-17T19:03:59.247 に答える
0

補間してみる

class="#{alert_color(name)}"
于 2013-10-17T18:49:56.533 に答える