I've been trying to figure out how to use the current_user user_name as their url. Currently it looks like http://localhost:3000/users/2 instead of http://localhost:3000/users/user_name. All help is appreciated. For any questions, please ask.
User.rb
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :user_name, use: :slugged
User Controller changed all @user = User.friendly.find(params[:id) as shown below. Not the entire listing but you can see what I mean.
class UsersController < ApplicationController
def index
@user = User.new
if @current_user
@leaders = @current_user.leaders
end
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
cookies[:user_id] = @user.id
flash[:notice] = "Welcome"
redirect_to "/users/#{ @user.id }"
else
flash[:alert] = @user.errors.full_messages
redirect_to "/users/new"
end
end
def new
@user = User.new
end
def edit
@user = User.friendly.find(params[:id])
current_user
end
def show
@user = User.friendly.find(params[:id])
if current_user
@followerlink = DateRate.where(leader_id: @user.id,
follow_id: current_user.id).first
@reverselink = DateRate.where(leader_id: current_user.id,
follow_id: @user.id).first
end
end
def update
@user = User.friendly.find(params[:id])
if @user.update(user_params)
flash[:notice] = "Updated profile"
redirect_to "/users/#{ @user.id }"
else
flash[:alert] = @user.errors.full_message
end
redirect_to "/users/#{ @user.id }/edit"
end
Schema
create_table "users", force: true do |t|
t.string "user_name"
t.string "email"
t.string "password_digest"
t.text "about_me"
t.string "slug"
end
add_index "users", ["slug"], name: "index_users_on_slug", unique: true
If I should add more files in order to have a better understanding, please let me know. Thank you for any help given.
EDIT Including Rake Routes
root GET / users#index
GET /users/:id/about(.:format) users#about
GET /users/:id/photos(.:format) users#photos
GET /users/:id/questions(.:format) users#questions
GET /users/:id/date_rate(.:format) users#date_rate
users_matches GET /users/matches(.:format) users#matches
users_quick_match GET /users/quick_match(.:format) users#quick_match
GET /users/:id/settings(.:format) users#settings
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
Normally, It should work with
user_path(current_user)
But more specifically, try this,
user_path(current_user.to_param)