Search code examples
ruby-on-railsrubydeviseslug

How to add a slug to username for Devise?


Usually, I create slugs like this:

params[:user][:name_slug] = params[:user][:name].parameterize

But this quite problem with Devise - how can I add it? I've created a controller where I specified attributes that are needed for creating a user, but how to add the slug there?

class RegistrationsController < Devise::RegistrationsController

  private
    def sign_up_params
      params.require(:user).permit(:name, :name_slug, :email, :password, :password_confirmation)
    end

  protected
    def update_resource(resource, params)
      resource.update_without_password(params)
    end
end

Thank you in advance.


Solution

  • You could do that in your User model:

    before_validation :generate_slug
    
    # ...
    
    private
    def generate_slug
      self.name_slug = name.to_s.parameterize
    end