JobRetroller #create中的ActiveRecord :: StatementInvalid

时间:2012-06-21 20:05:36

标签: ruby-on-rails

当我从下面的job.rb模型中删除Devise模块时,遇到jobs_controller.rb的问题:

class Job < ActiveRecord::Base

# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable

    devise  :database_authenticatable,
            :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :contact_email, :contact_phone, :description, :district, :due_date, :expiration_date, :job_title, :posting_date, :requirements, :salary, :submission_process

end

然后我删除了Devise模块,并得到了结果模型:

class Job < ActiveRecord::Base

    # Setup accessible (or protected) attributes for your model
attr_accessible :contact_email, :contact_phone, :description, :district, :due_date,       :expiration_date, :job_title, :posting_date, :requirements, :salary, :submission_process

end

进行此转换时抛出的错误显示在以下跟踪中:

ActiveRecord::StatementInvalid in JobsController#create

SQLite3::ConstraintException: constraint failed: INSERT INTO "jobs" ("contact_email",     "contact_phone", "created_at", "current_sign_in_at", "current_sign_in_ip", "description", "district", "due_date", "email", "encrypted_password", "expiration_date", "job_title", "last_sign_in_at", "last_sign_in_ip", "posting_date", "remember_created_at", "requirements", "reset_password_sent_at", "reset_password_token", "salary", "sign_in_count", "submission_process", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

app/controllers/jobs_controller.rb:49:in `block in create'
app/controllers/jobs_controller.rb:48:in `create'

我有一个用户模型和一个工作模型,该应用程序包含一个基本的脚手架,允许用户发布和查看作业,这取决于使用Devise进行适当的身份验证。我对正确的后端构造相对较新,并且对前端,视图类型的Rails东西有更多的经验。我不确定我是否需要在作业模型中存在Devise,或仅在User模型中存在。

1 个答案:

答案 0 :(得分:0)

一般来说,像这样的异常意味着您违反了数据库约束。您表上的NOT NULL,UNIQUE或其他类型的数据库实施。

例如,Devise强制执行电子邮件和加密密码等字段不为空。 您可以查看db/schema.rb以查看为表格构建的约束和索引。

在这种情况下,您很可能没有填充encrypted_password属性和/或您正在插入重复的电子邮件。 You can remove these constraints with a migration if you need

编辑:在回答第二个问题时,您只需要设计用户进行身份验证的模型(存储密码的位置)。然后,要强制用户只能创建和编辑属于他们的作业,请在控制器中说:before_filter :authenticate_user!并在您的个人操作中(或在另一个before_filter中)检查他们尝试编辑的作业是否属于他们。