简体   繁体   中英

Rails 4: Error messages not displaying with Bootstrap classes

In my application.html.erb file, I have:

<div class="container">
  <% flash.each do | type, message | %>
  <div class="alert <%= flash_class(type) %>">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span></button>
      <%= message %>
  </div>
  <% end %>

  <%= yield %>
</div>

I have the flash_class as a helper method:

module ApplicationHelper
  def flash_class(type)
    case type
    when :alert
     "alert-error"
    when :notice
     "alert-success"
    else
     ""
    end
  end
end

Though what happens is the class isn't being appended in the HTML. When I create an error, the 'alert-error' class isn't added on:

在此处输入图片说明

Any ideas why that might be the case?

You might be sending in String for type instead of Symbol in the flash_class method.

An easy fix would be calling type.to_sym in the flash_class helper method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM