ruby on rails - 找不到具有' id' =的用户

时间:2017-03-26 21:06:58

标签: ruby-on-rails

我正在创建一个博客,我希望通过点击"上传器"来显示包含特定用户的所有帖子的个人资料。 index.html.erb中的链接(第9行)。我使用了名为Pages的控制器并在其中定义了配置文件并将其链接到" uploader"并传递了该帖子的用户。

code screenshot

我收到错误"无法找到用户' =' ="

error screenshot

终端显示用户ID为nil

Started GET "/" for 127.0.0.1 at 2017-03-27 02:08:49 +0530
Processing by PostsController#index as HTML
  Post Load (0.5ms)  SELECT "posts".* FROM "posts"  ORDER BY created_at DESC
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 4]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Rendered posts/index.html.erb within layouts/application (10.8ms)
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
Completed 200 OK in 84ms (Views: 81.4ms | ActiveRecord: 1.1ms)


    Started GET "/pages/profile.3" for 127.0.0.1 at 2017-03-27 01:49:02 +0530
    Processing by PagesController#profile as 
      User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", nil]]
    Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)

    ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
      app/controllers/pages_controller.rb:3:in `profile'

我做错了什么? 有没有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

你的问题是路线。这样:

GET "/pages/profile.3"

应该是这样的:

GET "/pages/profile/3"

这是由您的路线缺少必需参数引起的。将其更改为

# routes.rb
get 'pages/profile/:id

它应该有效。