我正在使用使用用户名的虚URL来创建用户个人资料。实际的配置文件页面运行良好,但是如果用户已登录,我将我的根页面作为配置文件页面。如果他们被重定向到root,那么他们会收到错误Couldn't find User without an ID
并将此代码显示为指向@user line ...
class ProfilesController < ApplicationController
def show
@current_user = current_user
@user = User.friendly.find(params[:id])
@username = "@" + @user.username
@posting = Posting.new
end
end
这是我的路线文件......
devise_for :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
get "profiles/show"
devise_scope :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
get '/edit', to: 'devise/registrations#edit', as: :edit
end
authenticated :user do
devise_scope :user do
root to: "profiles#show", :as => "authenticated"
end
end
unauthenticated do
devise_scope :user do
root to: "devise/sessions#new", :as => "unauthenticated"
end
end
get '/:id' => 'profiles#show', as: :profile
答案 0 :(得分:1)
这不是友好id gem的问题。问题是您在没有提供id的情况下重定向到show方法,因此params[:id]
为nil。
您可以通过更改show
方法来解决此问题:
def show
@current_user = current_user # why do you need this?
@user = params[:id] ? User.friendly.find(params[:id]) : current_user
@username = "@" + @user.username
@posting = Posting.new
end