2015-08-15 103 views
0

我一直在想如何使用current_user user_name作爲它們的url。目前看起來像http://localhost:3000/users/2而不是http://localhost:3000/users/user_name。所有的幫助表示讚賞。如有任何問題,請詢問。friendly_id gem for current_user

User.rb

class User < ActiveRecord::Base 
extend FriendlyId 
friendly_id :user_name, use: :slugged 

用戶控制器改變所有@user = User.friendly.find(PARAMS [:ID),如下所示。不是整個列表,但你可以看到我的意思。

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 "https://stackoverflow.com/users/#{ @user.id }" 
    else 
    flash[:alert] = @user.errors.full_messages 
    redirect_to "https://stackoverflow.com/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 "https://stackoverflow.com/users/#{ @user.id }" 
    else 
    flash[:alert] = @user.errors.full_message 
    end 
    redirect_to "https://stackoverflow.com/users/#{ @user.id }/edit" 
    end 

模式

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 

如果我要爲了增加更多的文件有一個更好的瞭解,請讓我知道。感謝您給予的任何幫助。

編輯 包括耙路線

   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 
+0

可以顯示'rake routes | grep用戶? –

+0

@NicolasMaloeuvre我添加了我的'耙路線'。 –

+0

檢查分貝,是slu being被保存?我懷疑,slu is是零 – mudasir

回答

0

常,它應與

user_path(current_user) 

但更具體,試試這個工作,

user_path(current_user.to_param) 
+0

謝謝利奧。現在完美運作。你對於user_path(current_user)是正確的。再次感謝你的幫助。 –

1

回答你的問題

如何使用CURRENT_USER USER_NAME作爲他們的網址

形成你的查詢
User.friendly.find_by_user_name(params[:user_name]). 

就像這個...

#config/routes.rb 
get "/users", to: "users#index" 
get "https://stackoverflow.com/users/:user_name", to: "users#show" 
get "https://stackoverflow.com/users/new", to: "users#new" 
get "https://stackoverflow.com/users/:user_name/edit", to: "users#edit" 

# construct your new form with post method for this to work 
post "https://stackoverflow.com/users/create", to: "users#create" 
# construct your edit form with post method for this to work 
post "https://stackoverflow.com/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 "https://stackoverflow.com/users/#{ @user.user_name }" 
    else 
    flash[:alert] = @user.errors.full_messages 
    redirect_to "https://stackoverflow.com/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 "https://stackoverflow.com/users/#{ @user.user_name }" 
    else 
    flash[:alert] = @user.errors.full_message 
    end 
    redirect_to "https://stackoverflow.com/users/#{ @user.user_name }/edit" 
end 
0

您應該使用user_path(ID:user.name)或edit_user_path(ID:user.name),例如,以用戶的URL