Saving custom fields in devise User model in rails 4

前端 未结 4 985
名媛妹妹
名媛妹妹 2021-02-05 20:24

I made a devise User model and added additional fields to it. When I create and account everything works fine, only with email, pw and pw conf.

I then want to allow the

相关标签:
4条回答
  • 2021-02-05 21:05

    After working on something similar to this, I settled on using Application Controller, then afterward found that the Devise Documentation is fairly straightforward for this in their strong parameters section and gives an alternative to using Application Controller. https://github.com/plataformatec/devise#strong-parameters

    Below is the approach with Application Controller which worked for me.

        class ApplicationController < ActionController::Base
    
          before_filter :configure_permitted_parameters, if: :devise_controller?
    
          private
    
          def configure_permitted_parameters
             devise_parameter_sanitizer.for(:sign_up){ |u| u.permit(:name, :username, :about,  :email, :password, :password_confirmation)}        
             devise_parameter_sanitizer.for(:account_update){ |u| u.permit(:name, :username, :about, :email, :password, :password_confirmation) }          
          end
    
        end
    

    This should work the same and it directly overwrites methods in Devise::RegistrationController.

        class Users::RegistrationsController < Devise::RegistrationsController
    
          private
    
          def configure_sign_up_params
            devise_parameter_sanitizer.for(:sign_up){ |u| u.permit(:name, :username, :about,  :email, :password, :password_confirmation)}
          end
    
          def configure_account_update_params
            devise_parameter_sanitizer.for(:account_update){ |u| u.permit(:name, :username, :about, :email, :password, :password_confirmation) }
          end
    
        end
    
    0 讨论(0)
  • 2021-02-05 21:06

    In Rails 4.2, this is how I did. I have User Model on which devise is applied.
    Use this command "rails generate devise:controllers users" to generate custom controllers.
    I have added "username" name attribute to my User Model

    In my controller

    class Users::RegistrationsController < Devise::RegistrationsController
     before_filter :configure_sign_up_params, only: [:create]
     before_filter :configure_account_update_params, only: [:update]
     #rest of code as generated
    
       protected
    
        # If you have extra params to permit, append them to the sanitizer.
        def configure_sign_up_params
          devise_parameter_sanitizer.for(:sign_up) << :username
        end
    
       # If you have extra params to permit, append them to the sanitizer.
       def configure_account_update_params
         devise_parameter_sanitizer.for(:account_update) << :username
       end
    

    In Routes

    devise_for :users, controllers: {registrations: "users/registrations"}
    
    0 讨论(0)
  • 2021-02-05 21:16

    First produce new field. for reference http://guides.rubyonrails.org/migrations.html

    Do you have add your new fields in user controller parameter?

       def user_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end
    
        def sign_up_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end
    

    In the application controller

    before_filter :configure_permitted_parameters, if: :devise_controller?
    
      protected
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation)}
    
        devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :password_confirmation)}
      end
    

    In your registration form that override devise add this

    class Users::RegistrationsController < Devise::RegistrationsController
         skip_before_filter :verify_authenticity_token, :only => [:ipn_notification]
      def sign_up_params
          params.require(:user).permit(:email, :password, :password_confirmation)
      end
    

    After that add your new fields in all views _form,show,edit,index.

    0 讨论(0)
  • 2021-02-05 21:21

    In Rails4 we have strong parameters so please

    Add following line to your application_controller.rb

    before_filter :configure_devise_params, if: :devise_controller?
      def configure_devise_params
        devise_parameter_sanitizer.for(:sign_up) do |u|
          u.permit(:first_name, :last_name, :gender, :email, :password, :password_confirmation)
        end
      end
    
    0 讨论(0)
提交回复
热议问题