繁体   English   中英

“找不到具有ID的无ID用户”和Friendly_ID gem

[英]`Couldn't find User without an ID` with Friendly_ID gem

我正在使用使用用户名的个性网址创建用户个人资料。 实际的个人资料页面运行良好,但是如果用户已登录,我会将我的根页面作为个人资料页面。如果将他们重定向到根目录,则会收到错误消息: Couldn't find User without an ID并将此代码显示为错误指向@user行...

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

这不是友善的id gem的问题。 问题是您要重定向到show方法而不提供id,因此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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM