【问题标题】:Contact form: error on seeding and error messages of the form don't work properly联系表格:播种错误和表格错误消息无法正常工作
【发布时间】:2015-06-06 13:51:49
【问题描述】:

我正在构建的应用包含一个联系表单。在播种时,它会生成一条我不明白的错误消息:

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 行,它会成功播种(我不知道为什么这对播种有影响)。但即使在开发服务器上,如果我去联系表单并提交没有任何值,错误消息似乎无法正常工作。无论错误是什么,它只会给出一个错误消息“传递此消息时发生错误”,而对于所有其他形式,错误消息会指定变量和错误类型。什么可能导致这种行为?

联系表格的模型文件:

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

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

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

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

  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 消息可能会覆盖其他更具体的错误消息。删除此行并提交无效信息后,表单只会将无效字段变为红色,但根本不显示任何错误消息。例如,在其他形式中,我会收到这些类型的错误消息:

 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)

【问题讨论】:

  • 检查new 模板并确保您在该模板中呈现错误。

标签: ruby-on-rails ruby ruby-on-rails-4 model seeding


【解决方案1】:

添加attr_accessor :name相当于如下代码

def name
  @name
end

def name=(val)
  @name = val
end

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

【讨论】:

  • 啊,谢谢,我确实很难理解attr_accessor 的用途。鉴于您的解释,我想在我的情况下不需要 who attr_accessor 行,这解决了/解释了播种问题。我遵循的示例没有将联系消息写入数据库,我现在明白这是他们确实需要attr_accessor line 的原因。然而,这并不能解释在提交带有无效信息的表单时错误消息的问题。您是否也知道是什么原因造成的?
  • 是的,没错。 ActiveRecord 默认为所有数据库列添加了 setter 和 getter 方法,因此您不需要这样做。对于另一个错误,没有看到堆栈跟踪就没有任何线索。
  • 我不确定如何获取堆栈跟踪。日志文件也有帮助吗?我将此添加为对原始帖子的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多