2017-03-03 43 views
1

我一直在試圖自定義設計註冊方法來註冊更多的參數,也更新更多(目前爲止沒有運氣),但我總是得到Unpermitted parameters:錯誤。我試過使用這Adding extra registration fields with Devisehttps://github.com/plataformatec/devise#strong-parameters,但我不能克服。添加自定義參數來設計註冊 - 未經允許的參數

我也想過創建一個新表來容納一個外鍵的用戶ID,並把它放在那裏像user_id, display_name, profile_picture這樣的東西,但我會有同樣的問題,當試圖從同一頁提交的一切(混亂與設計控制器)。

你對我如何解決這個問題有任何建議嗎?還有什麼我必須發佈?

的routes.rb

devise_for :users, controllers: { registrations: 'users/registrations' } 

用戶/ REGC

def create 
    build_resource(registration_params) 

    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? 
     respond_with resource, :location => after_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords 
     respond_with resource 
    end 
    end 

private 
    def registration_paramss 
    params.require(:user).permit(:email, :display_name, :terms_of_services, :profile, :password, :password_confirmation) 
    end 
+0

請張貼你的設計配置。 – 31piy

+0

@ 31piy更新了它 –

+0

您使用的是什麼版本的設計?你能告訴我們重寫控制器(用戶/註冊)嗎?只需創建操作和您自定義註冊參數的部分即可。 – gkats

回答

6

看起來像你只需要告訴設計出的參數應該被允許。默認情況下,設計允許電子郵件(或用戶名,取決於配置),密碼和password_confirmation參數。你只需要添加更多。

devise documentation暗示了一種「懶惰的方式」來設置它。

class ApplicationController < ActionController::Base 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:display_name]) 
    end 
end 

的文檔,然後說,

If you have nested attributes (say you're using accepts_nested_attributes_for), then you will need to tell devise about those nestings and types. 

只有當你需要重寫registrations#create動作,你應該爲你的色器件定製路線。在這種情況下,請確保您也覆蓋了sign_up_params方法。

class Users::RegistrationsController < Devise::RegistrationsController 
    def create 
    # Your custom code here. Make sure you copy devise's functionality 
    end 

    private 

    # Notice the name of the method 
    def sign_up_params 
    params.require(:user).permit(:display_name, :email, :password, :password_confirmation) 
    end 
end 

在本質上,你就必須考慮如何使你的註冊表格被髮布的參數弄清楚如何在控制器配置強勁的參數。請確保您閱讀strong parameters語法。

希望它有幫助!

+0

我完全按照你的建議。同樣的錯誤。 –

+0

其實問題出在SessionsController中。在嘗試創建用戶後,它將嘗試登錄他,並且會話控制器仍然將參數視爲不允許。我是否應該修改會話控制器?這兩種解決方案都會遇到這個問題。 –

+0

您可以通過指定':sign_in'的參數來嘗試。但它似乎很奇怪,因爲據我所知設計不重定向,它只是註冊用戶。你得到的這個錯誤是錯誤還是警告?如果註冊工作並且您的模型已保存,則您不應該關心未經許可的參數警告。 – gkats

2

對於Devise 4.2.0,您可以通過將這些值添加到鍵來將用戶表的其他參數列入白名單。默認情況下,設計給你的評論,從現在開始。下面我加了:avatar

# If you have extra params to permit, append them to the sanitizer. 
    def configure_sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute, :avatar]) 
    end