gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:04:37 24 4
gpt4 key购买 nike

我一直试图在这里寻找答案,但我找不到任何有用的东西。我已经对我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com