rubyruby-on-rails-4sorcery

How do i add default data in rails strong params?


Lets say if i want to generate and set default password for user while creating them in admin.

and i want to do something like this,

 def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password=> @default_generated_password, :password_confirmation => @default_generated_password)
end

I am using sorcery for User Authentication. I am having a feeling that this is completely wrong way, But how do i do it?


Solution

  • Merge them in after

    params
     .require(:user)
     .permit(:first_name, :last_name)
     .merge(password: @default_generated_password, password_confirmation: @default_generated_password)
    

    Or if you need it just in one action (create for example)

    def create
      # password generation code...
      # ...
    
      @user = User.new(user_params.merge(password: @default_generated_password, password_confirmation: @default_generated_password))
    
      if @user.save
       redirect_to ...
      else
       flash[:error] = "Did not save"
       render :new
      end
    end
    
    private 
    
    def user_params
      params.require(:user).permit(:first_name, :last_name)
    end