Rails找不到没有id的用户

时间:2016-03-10 16:22:14

标签: ruby-on-rails params

好的,有些人请向我解释为什么会出现这个错误

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

1 个答案:

答案 0 :(得分:1)

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

这与您的控制器无法正确匹配。您应该更改routes.rb文件,以便将:user_id传递给控制器​​。您生成的路线应如下所示:

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

您可以使用resource或在routes文件中显式添加参数来实现此目的。请参阅resources routingbound parameters。我推荐前者。

如果您正在尝试创建用户的个人资料,我还建议将其保留在相同的控制器和资源路由命名空间中,而不是创建一个新的“接口”控制器。