2013-10-24 50 views
0

我已經安裝了Devise和CanCan,兩者似乎都在工作。我在遷移列「角色」,但是當我加入一個用戶後,檢查日誌,屋宇署查詢顯示沒有添加角色我無法將角色保存到我的設計用戶表(用於cancan)

完整的東西看起來像這樣的跡象:

Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"S+SjhxsALMbHkRBPPwOMIvHo1Bd0cNYl1g=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"receptionist"}, "commit"=>"Sign up"} 
Unpermitted parameters: role 
    (0.2ms) begin transaction 
    User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
Binary data inserted for `string` type on column `encrypted_password` 
    SQL (3.1ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$10$e3hKTibsZ7qSgOC5OelKgjumU8ufu46xNrBkmXcDEpix/m"], ["updated_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00]] 
    (0.7ms) commit transaction 
    (0.1ms) begin transaction 
Binary data inserted for `string` type on column `last_sign_in_ip` 
Binary data inserted for `string` type on column `current_sign_in_ip` 
    SQL (0.7ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 2 [["last_sign_in_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["current_sign_in_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["last_sign_in_ip", "127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updated_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00]] 
    (0.6ms) commit transaction 
Redirected to http://0.0.0.0:3000/ 
Completed 302 Found in 108ms (ActiveRecord: 5.6ms) 

此外,如果我編輯用戶並添加我獲取日誌中的以下錯誤

未經許可參數的作用:作用

我覺得遷移可能沒有奏效..但我可以訪問角色變量..所以我不完全理解。

我對軌相當新,所以我期待(並希望)我已經省略了一些明顯的東西。

在此先感謝

+0

是你用導軌3還是導軌4? –

+0

嗨,我正在使用rails 3 –

回答

0

從它的錯誤Unpermitted parameters:role意味着看起來你沒有在你的應用程序白名單這一點。我知道您的應用程序在處理屬性保護方面發生了變化。所以你需要whitelist這個參數。如果這是軌道4,你會做這樣的事情:

class User < ActionController::Base 
    def create 
    User.create(params[:user]) 
    end 

private 
    def user_params 
    params.require(:person).permit(:first_name, :last_name, :password, :role) 
    end 
end 

以上是隨Rails 4.然而,在你的情況(軌道3)我覺得你只需要做出這個屬性訪問的變化。因此,您可以嘗試將以下內容添加到您的用戶模型中。

attr_accessible :role 
0

在aplication控制器

 before_action :configure_permitted_parameters, if: :devise_controller? 
protected 
def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name,:role) } 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:password, :password_confirmation,:email,:name,:role) } 
end 

它爲我工作!

相關問題