简体   繁体   English

Rails 4-电子邮件验证错误消息

[英]Rails 4 - Error Messages for Email Validation

I am saving an email to the database and then sending it. 我正在将电子邮件保存到数据库,然后发送。 I am using the email model to validate the inputs, but I'm not sure how to display the error messages. 我正在使用电子邮件模型来验证输入,但是我不确定如何显示错误消息。 I am getting an undefined method `errors' for nil:NilClass error for the fullmessage.errors.any? 我为nil:nilClass错误获取了一个未定义的方法errors,该消息为fullmessage.errors.any? line in the index action(highlighted below) 索引动作中的行(下面突出显示)

View 视图

<%= form_tag("/thank_you") do %>
              <% if @fullmessage.errors.any? %> # <----- This line
                <h3>Errors</h3>
                <ul>
                <% @fullmessage.errors.full_messages.each do |message| %> # Would also cause an error if exemption not already raised
                  <li><%= message %></li>
                <% end %>
                </ul>
              <% end %>
              <div class="row">
                <div class="col-md-5">
                  <div class="form-group">
                      <%= text_field_tag :first_name, nil, class: 'form-control', placeholder: 'First Name' %>
                  </div>
                </div>
                <div class="col-md-7">
                  <div class="form-group">
                      <%= text_field_tag :last_name, nil, class: 'form-control', placeholder: 'Last Name' %>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="form-group">
                    <%= text_field_tag :email, nil, class: 'form-control', placeholder: 'Email Address' %>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="form-group text-area-wide">
                    <%= text_area_tag :message, nil, class: 'form-control text-area-wide', placeholder: 'When are you available?' %>
                  </div>
                </div>
              </div>
              <%= submit_tag 'Get Started', class: 'btn btn-success' %>
              <p><a href="http://www.skype.com/en/" target="_blank">Skype</a> required</p>
            <% end %>

Controller 控制者

def thank_you
    @first_name = params[:first_name]
    @last_name = params[:last_name]
    @email = params[:email]
    @message = params[:message] || "Hello!"
    @fullmessage = Email.create(first_name: @first_name, last_name: @last_name, email: @email, message: @message)
    if @fullmessage.valid?
      ActionMailer::Base.mail(
          :from => @email, 
            :to => 'erikvdw@comcast.net', 
            :subject => "A new contact form message from #{@first_name} #{@last_name}", 
            :body => @message).deliver
    else
      redirect_to root_path
      flash[:alert] = 'There was an issue with your submission'
    end
  end

Model 模型

class Email < ActiveRecord::Base
  validates_length_of :first_name, :maximum => 25, :minimum => 2
  validates_length_of :first_name, :maximum => 30, :minimum => 2
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  validates_length_of :message, :maximum => 500, :minimum => 20
end

Nil class means that the @fullmessage is nil and hasn't been set, and nil does not have the method errors, hence the error. Nil类表示@fullmessage为nil并且尚未设置,并且nil没有方法错误,因此是错误。

When you redirect, the @fullmessage value will have nothing because http is stateless. 重定向时,@ fullmessage值将没有任何内容,因为http是无状态的。 Absent caching and cookies, every variable is recreated with every request. 由于没有缓存和cookie,因此每个请求都会重新创建每个变量。 think what you are trying to do is display the flash messages, if that is the case you can add the @fullmessage.errors to the flash and be able to display that on the redirect. 认为您要执行的操作是显示Flash消息,如果是这种情况,可以将@ fullmessage.errors添加到Flash并能够在重定向中显示它。

You have two options: 您有两种选择:

  1. Use render instead of redirect to (this way you'll keep your @fullmessage object. 使用render而不是重定向到(通过这种方式,您将保留@fullmessage对象。
  2. Save error into flash[:alert] and display it in the view 将错误保存到flash[:alert]并在视图中显示

The 2nd option is more universal, you could add the relevant code to the layout and use it across the whole site. 第二个选项更为通用,您可以将相关代码添加到布局中,并在整个站点中使用它。 Eg: 例如:

<% if !flash.empty? %>
    <div id="flash">
      <% flash.keys.each do |k| %>
          <div class="alert alert-<%= k %>">
            <%= flash[k] %>
          </div>
      <% end %>
    </div>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM