联络表格:播种错误和表格错误讯息无法正常运作 [英] Contact form: error on seeding and error messages of the form don't work properly

查看:183
本文介绍了联络表格:播种错误和表格错误讯息无法正常运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建的应用程序包括联系表格.在播种时会生成我不理解的错误消息:

The app I'm building includes a contact form. On seeding it generates an error message that I don't understand:

ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (1, null, null, null, 2015-06-06 13:43:12.339477, 2015-06-06 13:43:12.339477).
: INSERT INTO "messages" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"

下面是表单的相关代码.如果我从模型文件中删除行attr_accessor :name, :email, :content,它将成功播种(我不知道为什么播种会有所不同).但是即使在开发服务器上,如果我转到联系表单并提交任何值都没有,则错误消息似乎仍无法正常运行.不管错误是什么,它都只给出一条错误消息传递此消息时发生了错误",而对于所有其他形式,错误消息都指定了错误的变量和类型.可能是什么原因导致了这种行为?

Below is the relevant code for the form. If I remove the line attr_accessor :name, :email, :content from the model file it succesfully seeds (I have no idea why this makes a difference for seeding). But even then on the development server, if I go to the contact form and submit without any values, the error messages don't seem to function properly. No matter the error it just gives a single error message "An error occurred while delivering this message", while for all other forms, the error message specifies the variable(s) and type of error. What could be causing this behavior?

联系表的模型文件:

class Message < ActiveRecord::Base
  attr_accessor :name, :email, :content
  validates :name,      presence: true,
                        length: { maximum: 255 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email,     presence: true,
                        length: { maximum: 255 },
                        format: { with: VALID_EMAIL_REGEX }
  validates :content,   presence: true,
                        length: { maximum: 600 }
end

迁移文件包括:

  t.string :name,       null: false,    limit: 255
  t.string :email,      null: false,    limit: 255
  t.string :content,    null: false,    limit: 600

在我的种子文件中,我有:

In my seeds file I have:

Message.create!(email: "example@example.com",
                name:  "Example User",
                content: "This is my message")


更新:关于错误消息,我具有以下控制器方法:


Update: Regarding the error message, I have the following controller method:

  def create
    @message = Message.new(message_params)
    if @message.valid?
      MessageMailer.new_message(@message).deliver_now
      flash[:success] = "Your messages has been sent."
      redirect_to contact_path
    else
      flash[:alert] = "An error occurred while delivering this message."
      render 'new'
    end
  end

我删除了闪光警报行.我拥有的其他表格也可以正常运行,但也没有此行,尽管此Flash消息可能会覆盖其他更具体的错误消息.删除此行并提交无效信息后,该表单仅使无效字段变为红色,但根本不显示任何错误消息.例如,以其他形式,我会得到以下类型的错误消息:

I removed the flash alert line. Other forms that I have that do function properly, don't have this line either and I though this flash message might be overwriting the other, more specific error messages. After removing this line and submitting invalid information, the form just makes the invalid fields red but shows no error messages at all. In other forms I would for example get these kinds of error messages:

 The form contains 5 errors.
    Email can't be blank
    Email is invalid
    Username can't be blank
    Username is too short (minimum is 6 characters)
    Username is invalid

日志文件为:

Started GET "/contact" for xx.xxx.xx.xxx at 2015-06-06 14:15:43 +0000
  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by MessagesController#new as HTML
  Rendered messages/new.html.erb within layouts/application (493.2ms)
  Rendered layouts/_shim.html.erb (0.4ms)
  Rendered layouts/_header.html.erb (10.5ms)
  Rendered layouts/_footer.html.erb (0.8ms)
Completed 200 OK in 1454ms (Views: 1429.3ms | ActiveRecord: 3.1ms)

Started POST "/contact" for xx.xxx.xx.xxx at 2015-06-06 14:15:49 +0000
Processing by MessagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zAxxxSKMYKey/jxmqqvxxxr***43g2Ud9r6SQ==", "message"=>{"name"=>"", "email"=>"", "content"=>""}, "commit"=>"Send"}
  Rendered messages/new.html.erb within layouts/application (7.3ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (3.2ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 849ms (Views: 844.9ms | ActiveRecord: 0.0ms)

推荐答案

添加attr_accessor :name等效于以下代码

def name
  @name
end

def name=(val)
  @name = val
end

因此,如果数据库中有一个名为name的列,则不会写入该列.那就是这里发生的事情.由于将这些列设置为null: false,因此如果它们为null,则postgres会引发错误.

So if you have a column named name in your database, it will not be written. That is what happens here. Since you set those columns to be null: false, postgres raises an error if they are null.

这篇关于联络表格:播种错误和表格错误讯息无法正常运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆