Rails/Bootstrap - Flash通知:成功现在是红色而不是绿色?

RuN*_*ruN 6 ruby flash ruby-on-rails ruby-on-rails-3 twitter-bootstrap

我一直试图在这里寻找答案,但我找不到任何有用的东西.我已经实现了:成功和:我的rails应用程序的危险闪光通知.它完全正常工作,即:成功是绿色的:危险是红色的,有一个关闭按钮和所有,但是因为添加一些邮件文件我的:成功现在显示红色?

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>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

我再说一遍,在邮件进入之前,这一切都完全正常......但现在我只是感到困惑.请帮忙!

Jus*_*tin 8

有时你会想使用超过noticesuccess,如引导警报 info,dangerwarning.

这是我建议的解决方案:

<% 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 %>
Run Code Online (Sandbox Code Playgroud)

这样,当你打电话flash[:success] = 'foo',你key会是success,同样为info,warning,danger等等.这样,您就可以利用所有不同的引导警报.

使用此方法,如果要使用语法notice: 'hello world',alert: 'oops'重定向,则必须添加另外两个扩展Bootstrap类的CSS类redirect_to root_url, notice: 'welcome home'.

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

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

.alert-notice {
  @extend .alert-warning;
}
Run Code Online (Sandbox Code Playgroud)

由于我之前关于邮件回调的评论更像是一个侧面说明而且与这个问题无关,所以我为你做了一个简单的要点.