2014-03-04 81 views
3

我使用cancan(1.6.10)和devise(3.2.2),我一直在按照cancan作者的建議,使用this guide來實現授權,我需要分配多個角色給用戶,然後我決定使用位掩碼將其存儲到單個整數列中(我向用戶模型添加了一個名爲「roles_mask」的列)。基於角色的cancan授權不起作用Rails 4 - Ruby 2.1

我有這樣的文件涉及:

  1. user.rb
  2. edit.html.erb

我知道,我本指南除了我寫的行字folowed字表明角色是一個可訪問的屬性:

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :roles) } 
    end 
end 

更新用戶(記錄)時,所有字段都會更新,除了:roles_mask = /,我不明白爲什麼角色字段未被捕獲。我認爲有些東西是我無法看到的。任何人都可以幫助我?

* 解決方案

使用和改變工作application_controller.rb文件cancancan(Rails的4支持)(如角色是一個非標量的屬性)。

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, roles: []) } 
    end 
end 

回答

5

慘慘不支持的Rails 4.但是,您可以使用CanCanCan,這是Rails4友好:https://github.com/CanCanCommunity/cancancan

+0

感謝您的建議,我並沒有注意到這一細節......我權且你以後。 – geoom

+0

以及... @rails4guides.com,似乎問題仍然在那個寶石,我只是報告給cancancan comunity(https://github.com/CanCanCommunity/cancancan/issues/11)..一些想法? – geoom

+1

嘗試在強參數中定義你的角色作爲一個數組:角色:[] –