【问题标题】:Flash messaging is being named incorrectly when using bootstrap and simple form使用引导程序和简单表单时,Flash 消息被错误命名
【发布时间】:2012-08-26 23:36:56
【问题描述】:

我不确定为什么会发生这种情况,但是当我在我的 rails 应用程序中成功提交表单时,它会在我成功的 flash 消息中添加一个错误的 div 类。我正在将此代码用于我的 Flash 消息:

<% flash.each do |name, msg| %>
   <div class="alert alert-<%= name == :notice ? "success" : "error" %>">
      <a class="close" data-dismiss="alert">&#215;</a>
      <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
   </div>
<% end %>

这正是 Ryan Bates 在这个 rails cast 中建议的做法:

http://railscasts.com/episodes/329-more-on-twitter-bootstrap?view=asciicast

该代码有什么问题?如果我只是这样做,它就可以正常工作:

<% flash.each do |name, msg| %>
  <div class="alert alert-<%= name %>">
    <a class="close" data-dismiss="alert">&#215;</a>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  </div>
<% end %>

但这似乎不是正确的方法。我不明白为什么 railscast 中建议的代码不起作用。

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap simple-form


    【解决方案1】:

    当您在控制器中创建 Flash 消息时,您可以使用字符串 'notice' 而不是符号 :notice。尝试在您的 application.html.haml 中添加这些行,以查看您从 Flash 消息中得到什么

    - if Rails.env.development?
      = debug(params) 
      = debug(flash) unless flash.empty?
    

    【讨论】:

    • 我添加了调试消息,它显示:skitch.com/leemcalilly/eu4dn/the-fuzz-cloud
    • 您的flash 包含:success 但不包含:notice,因此请将您的代码更改为:success ? "success" : "error"
    • 我认为这是问题所在。
    【解决方案2】:

    据我所知,flash 有时包含空元素。在迭代之前检查flash 中是否有内容。

    【讨论】:

    • 先检查flash是否为空无济于事,因为flash不是空的,它只是将alert-error类添加到div中
    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2013-12-14
    相关资源
    最近更新 更多