0

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
Jake
  • 462
  • 1
  • 5
  • 13
  • could you show 'rake routes | grep users' ? – Nicolas Maloeuvre Aug 15 '15 at 18:41
  • @NicolasMaloeuvre I've added my 'rake routes'. – Jake Aug 15 '15 at 19:12
  • check in db, is slug being saved? i doubt, slug is nil – Raza Aug 15 '15 at 19:29
  • @leo Hello. When I check my users information in console, slug is being saved. An example is a user_name:yama12 and slug:yama12. It works when i view other users pages but not for the current_user – Jake Aug 15 '15 at 20:05
  • u mean, it works fine on user_path(@user), while it doesn't work for user_path(current_user) ? – Raza Aug 15 '15 at 20:23
  • @leo That's exactly correct. When I visit a User profile when I'm logged in I get for example /users/username but if I check my own profile it says /users/1. – Jake Aug 15 '15 at 21:46
  • well, check my answer below, let me know if it works – Raza Aug 16 '15 at 03:04

3 Answers3

1

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
MilesStanfield
  • 4,571
  • 1
  • 21
  • 32
0

You should use user_path(id:user.name) or edit_user_path(id:user.name) for example in order to user those url

Nicolas Maloeuvre
  • 3,069
  • 24
  • 42
0

Normally, It should work with

user_path(current_user)

But more specifically, try this,

user_path(current_user.to_param)
Raza
  • 2,320
  • 2
  • 22
  • 32
  • Thank you Leo. Works perfectly now. You were right about user_path(current_user). Thanks again for all your help. – Jake Aug 16 '15 at 06:25