ruby-on-rails - Rails/Bootstrap - Flash 通知 :success is now red and not green?

标签 ruby-on-rails ruby ruby-on-rails-3 twitter-bootstrap flash

我一直试图在这里寻找答案,但我找不到任何有用的东西。我已经对我的 Rails 应用程序实现了 :success 和 :danger flash 通知。它工作得很好,即 :success 是绿色的, :danger 是红色的,有一个关闭按钮等等,但是自从添加了一些邮件文件后,我的 :success 现在显示为红色?

application.html.erb 摘录:

<body>

  <div class="container">
    <% flash.each do |key, value| %>
      <%= content_tag :div, class: "alert alert-#{key == 'notice ? 'success' : 'danger'}" do %>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <%= value %>
      <% end %>
    <% end %>

    <%= yield %>
  </div>

</body>

contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default to: 'justindavidson23@gmail.com'

  def contact_email(name, phone, email, event_type, body)
    @name = name
    @phone = phone
    @email = email
    @event = event_type
    @body = body

    mail(from: email, subject: 'Contact Form Message').deliver
  end
end

contacts_controller.rb

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      name = params[:contact][:name]
      phone = params[:contact][:phone]
      email = params[:contact][:email]
      event = params[:contact][:event_type]
      body = params[:contact][:comments]

      ContactMailer.contact_email(name, phone, email, event, body).deliver
      flash[:success] = 'Message Sent.'
      redirect_to new_contact_path
    else
      flash[:danger] = 'Error occurred, messgage not sent.'
      redirect_to new_contact_path
    end
  end
end

private
def contact_params
  params.require(:contact).permit(:name, :phone, :email, :event_type, :comments)
end

还有,contact_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>New Message from Hoot and Holla's Contact form, from <%= "#{@name}, #{@email}" %></p>
    <p><%= @phone %></p>
    <p><%= @event %></p>
    <p><%= @body %></p>
  </body>
</html>

我重复一遍,在邮件程序进入之前这一切都工作得很好......但现在我只是感到困惑。请帮忙!

最佳答案

有时您想要使用的不仅仅是noticesuccess,例如Bootstrap alerts信息危险警告

这是我推荐的解决方案:

<% flash.each do |key, value| %>
  <div class="alert alert-<%= key %> alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <%= value %>
   </div>
<% end %>

这样,当您调用 flash[:success] = 'foo' 时,您的 key 将是 success,对于 infowarningdanger 等。这样您就可以利用所有不同的 Bootstrap alerts .

使用此方法,您将不得不添加 2 个扩展 Bootstrap 类的 CSS 类,如果您想要使用语法 notice: 'hello world',alert: 'oops' 在你的重定向中,比如 redirect_to root_url, notice: 'welcome home'

如果你确实想使用这些,那么你可以使用 Sass,如下所示。

.alert-alert {
  @extend .alert-danger;
}

.alert-notice {
  @extend .alert-warning;
}

由于我之前对邮件回调的评论更多的是旁注,与这个问题无关,所以我做了一个 simple gist给你。

关于ruby-on-rails - Rails/Bootstrap - Flash 通知 :success is now red and not green?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31854717/

相关文章:

ruby-on-rails - 如果所有其他变量和输出都已知,是否可以求解 ruby​​ 中哈希的输入值? ( ruby )

ruby-on-rails - rails : Query on a Method

ruby-on-rails - 在哪些情况下您需要在设计上实现这些复杂的 token 身份验证(Rails 3.2)

ruby-on-rails - 如何在Spree中添加新角色?

ruby-on-rails - 如何在一个数组中多次重复单个散列?

ruby-on-rails - 当类位于子目录中并被子类化时,Rspec 因 load_missing_constant 而失败

ruby - 将 to_hash(或 to_h)方法添加到 Struct 的优雅方式?

ruby-on-rails - 如何创建枚举类型并默认为新对象的特定值

ruby - Rails has_many 关联导致 "no such column"错误

ruby-on-rails - rails 3 : What is the correct Rails way of doing custom types?