2016-03-10 54 views
0

好了,所以有人请向我解释这是为什么错误显示出来的Rails找不到用户

profile.html

<%= @user.username %> 

profile.controller

class InterfaceController < ApplicationController 
    #before_action :authenticate_user! 

    def profile 
     @user = User.find(params[:user_id]) 
    end 
end 

我大概读过堆栈溢出的每一篇文章都有同样的错误,但是他们中没有人帮助过我。我真的很感谢帮助和解释。

路线

Prefix Verb URI Pattern         Controller#Action 
     new_user_session GET /users/sign_in(.:format)     devise/sessions#new 
      user_session POST /users/sign_in(.:format)     devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)     devise/sessions#destroy 
      user_password POST /users/password(.:format)     devise/passwords#create 
     new_user_password GET /users/password/new(.:format)    devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format)    devise/passwords#edit 
         PATCH /users/password(.:format)     devise/passwords#update 
         PUT /users/password(.:format)     devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)      devise/registrations#cancel 
     user_registration POST /users(.:format)       devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)     devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)      devise/registrations#edit 
         PATCH /users(.:format)       devise/registrations#update 
         PUT /users(.:format)       devise/registrations#update 
         DELETE /users(.:format)       devise/registrations#destroy 
        root GET /           posts#index 
       profile GET /profile(.:format)       interface#profile 
      post_comments GET /posts/:post_id/comments(.:format)   comments#index 
         POST /posts/:post_id/comments(.:format)   comments#create 
     new_post_comment GET /posts/:post_id/comments/new(.:format)  comments#new 
     edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit 
      post_comment GET /posts/:post_id/comments/:id(.:format)  comments#show 
         PATCH /posts/:post_id/comments/:id(.:format)  comments#update 
         PUT /posts/:post_id/comments/:id(.:format)  comments#update 
         DELETE /posts/:post_id/comments/:id(.:format)  comments#destroy 
        posts GET /posts(.:format)       posts#index 
         POST /posts(.:format)       posts#create 
       new_post GET /posts/new(.:format)      posts#new 
       edit_post GET /posts/:id/edit(.:format)     posts#edit 
        post GET /posts/:id(.:format)      posts#show 
         PATCH /posts/:id(.:format)      posts#update 
         PUT /posts/:id(.:format)      posts#update 
         DELETE /posts/:id(.:format)      posts#destroy 
+2

'params [:user_id]'可能是'nil'。你的路线是什么样子的?告诉我'斌/耙routes' – sjagr

+0

检查服务器日志,它会告诉你哪些PARAMS提交 – prusswan

+0

请发表你的问题'则params的值:USER_ID]' –

回答

1
profile GET /profile(.:format)       interface#profile 

这并不控制器正确匹配。您应该更改routes.rb文件,以便将:user_id传递到控制器中。你得到的路线应该是这样的:

profile GET /profile/:user_id(.:format)       interface#profile 

您可以通过使用resource或路由文件中明确添加参数实现这一目标。请参阅resources routingbound parameters。我会推荐前者。

如果你想使用户的个人资料,我也建议保持它在同一个控制器和资源的路线命名空间内,而不是做一个新的“接口”控制器。

+0

我不太明白如何chenge它的routes.rb我目前的路线是这样获得的个人资料“,到:“接口#配置文件”,如:'profile' – KristjanT

+0

在这种情况下,您可以执行'get'profile /:user_id':'interface#profile',如:'profile'' – sjagr