2012-06-21 146 views
0

有一個問題在這裏與jobs_controller.rb,當我從我下面的job.rb模型去除設計模塊:的ActiveRecord :: StatementInvalid在JobsController#創建

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 

我然後取下設計模塊,並在到達結果模型:

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有更多的經驗。我不知道我是否需要在Job模型中存在Devise,或者只在User模型中。

回答

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)中檢查他們嘗試編輯的作業是否屬於他們。

相關問題