2015-08-30 27 views
0

在使用Devise和Admin模型的應用程序中,我需要添加一些字段 - 它們已成功添加。現在我還需要給用戶修改這些屬性的能力。當我打開視圖來修改這些參數併發送表單時,新添加的字段(如電話號碼,網站等)不會被修改。設計管理模型 - 如何允許參數(新字段)?

在終端輸出我看到這是因爲這些參數是不允許的,但我怎麼能允許他們?

在整個update過程中發生的操作是registrations#update

def update 
    @user = User.find(current_user.id) 
    successfully_updated = if needs_password?(@user, params) 
     @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update)) 
    else 
     params[:user].delete(:current_password) 
     @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update)) 
    end 

    if successfully_updated 
     flash[:notice] = "Your password has been successfully changed." 
     # Sign in the user bypassing validation in case their password changed 
     sign_in @user, :bypass => true 
     redirect_to edit_user_registration_path(:status => 'ok') 
    else 
     render "edit" 
    end 
    end 

卜這段代碼似乎是users,而不是admins - 我該如何解決這個問題呢?

預先感謝您。

+0

這實質上是在這裏找到答案:http://stackoverflow.com/a/16389077/1753596 - 在每個控制器,用戶和管理員(或任何你要添加額外的字段),您需要配置你自己的一套設計許可參數 – trh

+0

[Strong parameters with Rails 4.0 and Devise]可能的重複(http://stackoverflow.com/questions/16379554/strong-parameters-with-rails-4-0-and-devise ) – ifma

回答

0
class RegistrationsController < Devise::RegistrationsController 
    before_action :configure_permitted_parameters 

    # ... 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:account_update) << :username 
    end 
end 

在本示例中,我們將:username參數添加到白名單。

https://github.com/plataformatec/devise#strong-parameters

相關問題