我一直在想如何使用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
可以顯示'rake routes | grep用戶? –
@NicolasMaloeuvre我添加了我的'耙路線'。 –
檢查分貝,是slu being被保存?我懷疑,slu is是零 – mudasir