简体   繁体   中英

ruby on rails — Couldn't find User with 'id'=

I am creating a blog and I want to show profile containing all posts of a perticular user by clicking the "uploader" link in index.html.erb(line no. 9). I used a controller named Pages and defined profile in it and linked it to "uploader" and passed user of that post.

code screenshot

I am getting error "Couldn't find User with 'id'="

error screenshot

terminal is showing User id as 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'

What am I doing wrong? Any better method to do this?

Your problem is the route. This:

GET "/pages/profile.3"

should really be this:

GET "/pages/profile/3"

and that's caused by your route missing the required parameter. Change it to

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

and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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