2013-04-17 29 views
0

使用Rails 3.2和Devise,我已經用自定義的覆蓋了註冊控制器。我根本沒有更改create方法中的代碼,它是Devise中的原始代碼。電子郵件說是空的不是用Rails 3.2和Devise

但奇怪的是,每當我嘗試創建/註冊一個新用戶時,我總是收到這個錯誤。

PG::Error: ERROR: null value in column "email" violates not-null constraint 
: INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "last_sign_in_at", "last_sign_in_ip", "payroll", "sign_in_count", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" 

但是在它下面,一切似乎都沒問題,因爲用戶正在收到一封有效的電子郵件。

{"utf8"=>"✓", 
"authenticity_token"=>"8/ZKW11lF22WYKV2K36zBkSk6DSU36/1/zU54a2IRmM=", 
"user"=>{"username"=>"someguy", 
"email"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, 
"commit"=>"Sign up", 
"format"=>"user"} 

我不太清楚在這裏粘貼什麼其他代碼,因爲所有的東西顯然都適合我。所以請請求任何可能有幫助的東西。

這是我的註冊控制器,但create基本上有相同的代碼其super。錯誤發生在有resource.save的行。

class UserRegistrationsController < Devise::RegistrationsController 
    def new 
    super 
    end 

    def create 
    build_resource 

    if resource.save 

     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_up(resource_name, resource) 
     respond_with resource, :location => after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     respond_with resource 
    end 
    end 

    def update 
    super 
    end 
end 

顯然,所有的字段都是nil。這是它執行查詢:

INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "payroll", "sign_in_count", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["created_at", Wed, 17 Apr 2013 19:34:22 BST +01:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", nil], ["first_name", nil], ["last_name", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["payroll", nil], ["sign_in_count", 0], ["updated_at", Wed, 17 Apr 2013 19:34:22 BST +01:00], ["username", ""]] 
+1

請發佈您的'registrations_controller.rb' – benchwarmer

+0

@benchwarmer,我已將它附加到原始帖子。 – cgf

+0

你可以添加'build_resource'方法嗎?我假設它在你的超類'RegistrationsController'中? – covard

回答

0

是通過電子郵件發送大量分配上的用戶類?嘗試在User類中聲明attr_accessible:email。

+0

是的。當它試圖將它插入到數據庫中時,似乎所有的字段都以零的形式傳遞給Rails。 – cgf

+0

'attr_accessible:email'的解決方案是否有效? –

+0

不,對不起。它已經可以批量分配。 – cgf

相關問題