2017-03-26 3 views
0

인 사용자를 찾을 수 없습니다. 블로그를 만들고 있는데 index.html.erb에서 "업 로더"링크를 클릭하여 특정 사용자의 모든 게시물을 포함하는 프로필을 표시하고 싶습니다. 라인 9 번). 나는 Pages라는 컨트롤러와 정의 된 프로필을 사용하고 그것을 "업 로더"에 연결하고 그 게시물의 사용자를 전달했다.ruby ​​on rails - 'id'=

code screenshot

나는 점점 오전 오류가

error screenshot

터미널이 표시되어 사용자 ID 전무

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' 

으로 무엇 오전 " 'ID'= 사용하여 사용자를 찾을 수 없습니다" 내가 잘못하고있어? 더 좋은 방법은 무엇입니까?

답변

2

문제는 경로입니다. 이 :

GET "/pages/profile.3" 

정말이 있어야한다 :

GET "/pages/profile/3" 

을하고 필요한 매개 변수가없는 경로에 의해 발생합니다. 이 파일을

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

으로 변경하면 제대로 작동합니다.

+0

도 마찬가지입니다. 내 routes.rb를 업데이트하여 'pages/profile/: id'를 가져 왔습니다. 이제 오류가 발생했습니다. "누락되었습니다 : 경로 정의에 컨트롤러 키를 입력하십시오. (ArgumentError)" –

+0

일부 인터넷 검색 결과이 링크가 발견 된 후 (http://guides.rubyonrails.org/routing.html) 알아 냈습니다. 내 경로는 "get 'pages/profile/: id'=> 'pages # profile'과 같이 'profile'"이어야하며 <% = link_to "user profile"을 사용하여 코드에서 액세스 할 수 있습니다. profile_path (user) %> –