Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails / Bootstrap – Flash注意:成功现在是红色而不是绿色?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图在这里寻找答案,但我找不到任何有用的东西.我已经实现了:成功和:我的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>

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,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>

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

解决方法

有时候你会想要使用的不仅仅是通知和成功,比如 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’时,你的密钥就会成功,同样对于信息,警告,危险等等.这样你就可以利用所有不同的Bootstrap alerts.

使用此方法,如果要在重定向中使用语法通知:’Hello world’或alert:’oops’,则必须添加2个扩展Bootstrap类的CSS类,如redirect_to root_url,注意: ‘欢迎回家’.

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

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

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

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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – Rails / Bootstrap – Flash注意:成功现在是红色而不是绿色?全部内容,希望文章能够帮你解决ruby-on-rails – Rails / Bootstrap – Flash注意:成功现在是红色而不是绿色?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。