2017-03-26 196 views
0

我正在創建一個博客,我想通過單擊index.html.erb中的「uploader」鏈接來顯示包含特定用戶的所有帖子的配置文件第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'。現在我收到錯誤「Missing:controller key on routes definition,please check your routes。(ArgumentError)」 –

+0

經過一些Google搜索後,我發現這個鏈接(http://guides.rubyonrails.org/routing.html),我想通了我的路線應該是「get」pages/profile /:id'=>'pages#profile',如:'profile'「,我可以使用<%= link_to」user profile「,profile_path(user )%> –