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

时间:2022-10-10 12:15:16

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? line in the index action(highlighted below)

我正在保存一个电子邮件到数据库,然后发送它。我正在使用电子邮件模型来验证输入,但我不确定如何显示错误消息。我为nil获得了一个未定义的方法“错误”:fullmessage的NilClass错误。索引操作中的行(在下面突出显示)

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

2 个解决方案

#1


0  

You have two options:

你有两个选择:

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

The 2nd option is more universal, you could add the relevant code to the layout and use it across the whole site. E.g.:

第二个选项更通用,您可以将相关代码添加到布局中,并在整个站点中使用它。例如:

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

#2


0  

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. Absent caching and cookies, every variable is recreated with every request. 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.

当您重定向时,@fullmessage值将没有任何内容,因为http是无状态的。如果没有缓存和cookie,每个变量都将根据每个请求重新创建。想想你要做的是显示flash消息,如果是这样的话,你可以添加@fullmessage。对flash的错误,并能够显示在重定向上。

#1


0  

You have two options:

你有两个选择:

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

The 2nd option is more universal, you could add the relevant code to the layout and use it across the whole site. E.g.:

第二个选项更通用,您可以将相关代码添加到布局中,并在整个站点中使用它。例如:

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

#2


0  

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. Absent caching and cookies, every variable is recreated with every request. 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.

当您重定向时,@fullmessage值将没有任何内容,因为http是无状态的。如果没有缓存和cookie,每个变量都将根据每个请求重新创建。想想你要做的是显示flash消息,如果是这样的话,你可以添加@fullmessage。对flash的错误,并能够显示在重定向上。