Reputation: 502
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
Upvotes: 0
Views: 196
Reputation: 2388
Normally, It should work with
user_path(current_user)
But more specifically, try this,
user_path(current_user.to_param)
Upvotes: 0
Reputation: 3169
You should use user_path(id:user.name) or edit_user_path(id:user.name) for example in order to user those url
Upvotes: 0
Reputation: 4649
to answer your question
how to use the current_user user_name as their url
Form your queries with
User.friendly.find_by_user_name(params[:user_name]).
Like this ...
#config/routes.rb
get "/users", to: "users#index"
get "/users/:user_name", to: "users#show"
get "/users/new", to: "users#new"
get "/users/:user_name/edit", to: "users#edit"
# construct your new form with post method for this to work
post "/users/create", to: "users#create"
# construct your edit form with post method for this to work
post "/users/:user_name/update", to: "users#update"
def index
@user = User.new
if @current_user
@leaders = @current_user.leaders
end
end
def new
@user = User.new
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.user_name }"
else
flash[:alert] = @user.errors.full_messages
redirect_to "/users/new"
end
end
def edit
@user = User.friendly.find_by_user_name(params[:user_name])
current_user
end
def show
@user = User.friendly.find_by_user_name(params[:user_name])
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_by_user_name(params[:user_name])
if @user.update(user_params)
flash[:notice] = "Updated profile"
redirect_to "/users/#{ @user.user_name }"
else
flash[:alert] = @user.errors.full_message
end
redirect_to "/users/#{ @user.user_name }/edit"
end
Upvotes: 1