Scenerio:
1- User signs up with username elon_musk
2- Profile url is mywebsite.com/users/elon_musk
3- Elon edits his username to elonmusk
4- Now his profile url is mywebsite.com/users/elonmusk
It is all good so far, but I noticed mywebsite.com/users/elon_musk is also still available! Now Elon has 2 profile urls!
How can I delete elon_musk slug when he updates to elonmusk
Thank you!
User Table:
create_table "designers", force: :cascade do |t|
t.string "fullname"
t.string "website"
t.string "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "avatar"
t.string "slug"
t.string "twitter"
t.string "email", default: "", null: false
t.string "username"
t.string "location"
t.boolean "featured", default: false
t.string "twitter_username"
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.boolean "is_admin", default: false
t.string "provider"
t.string "uid"
t.index ["email"], name: "index_designers_on_email"
t.index ["reset_password_token"], name: "index_designers_on_reset_password_token", unique: true
end
omniauth_callbacks_controller
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
designer = Designer.from_omniauth(request.env['omniauth.auth'])
if designer.persisted?
sign_in_and_redirect designer, notice: "Signed in!"
else
session["devise.designer_attributes"] = designer.attributes
redirect_to new_designer_registration_url
end
end
alias_method :twitter, :all
end
registration_controller.rb
class RegistrationsController < Devise::RegistrationsController
def sign_up_params
params.require(:designer).permit(:username, :fullname, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:designer).permit(:username, :fullname, :email, :location, :website, :twitter, :bio, :featured, :is_admin, :password, :password_confirmation, :current_password)
end
protected
def after_sign_up_path_for(resource)
edit_designer_path(current_designer) if current_designer
end
end
designer.rb
class Designer < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:twitter]
has_many :posts
mount_uploader :avatar, AvatarUploader
extend FriendlyId
friendly_id :username, use: [:slugged, :history]
validates_presence_of :email # optional
validates_uniqueness_of :email # required
validates_presence_of :username # required
validates_uniqueness_of :username # required
validates_presence_of :password, if: :password_required? # recommended
validates_confirmation_of :password, if: :password_required? # recommended
validates_length_of :password, within: password_length, allow_blank: true # recommended
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |designer|
designer.provider = auth.provider
designer.uid = auth.uid
designer.slug = auth.info.nickname
designer.username = auth.info.nickname
designer.twitter_username = auth.info.nickname
designer.email = auth.info.email
designer.password = Devise.friendly_token[0, 20]
designer.fullname = auth.info.name
designer.remote_avatar_url = auth.info.image.sub("_normal", "") unless designer.avatar.present?
end
end
def self.new_with_session(params, session)
if session["devise.designer_attributes"]
new(session["devise.designer_attributes"]) do |designer|
designer.attributes = params
designer.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
def should_generate_new_friendly_id?
slug.blank? || username_changed?
end
end
Remove the :history from
friendly_id :username, use: [:slugged, :history]
http://www.rubydoc.info/github/norman/friendly_id/FriendlyId/History