0

这是我的 rake 路线中的相关行:

 client_note GET    /clients/:client_id/notes/:id(.:format)      notes#show

当我尝试传入<%= client_note_path([client, @notes.first]) %>>我得到的对象时:

No route matches {:action=>"show", :controller=>"notes", 
:client_id=>[#<Client id: 5, ... , #<Note id: 9, ...]}

这让我想到尝试客户端 ID。所以,我尝试了:<%= client_note_path([client.id, @notes.first]) %>

这给了我:

No route matches {:action=>"show", :controller=>"notes", :client_id=>[5, 
#<Note id: 9,content: "He just bought a brand new bobcat, be sure to charg...", 
client_id: 5, created_at: "2012-06-11 16:18:16", 
updated_at: "2012-06-11 16:18:16">]}

这让我想尝试只传递一个客户端 ID。<%= client_note_path(client.id) %>

No route matches {:action=>"show", :controller=>"notes", :client_id=>5}

仍然不是我要找的。我希望能够显示通常可以在 url 中找到的单个注释,如下所示:http://localhost:3000/clients/2/notes/3/

它期望什么对象?

完整的路线文件和耙路线

           users GET    /users(.:format)                             users#index
                 POST   /users(.:format)                             users#create
        new_user GET    /users/new(.:format)                         users#new
       edit_user GET    /users/:id/edit(.:format)                    users#edit
            user GET    /users/:id(.:format)                         users#show
                 PUT    /users/:id(.:format)                         users#update
                 DELETE /users/:id(.:format)                         users#destroy
        sessions POST   /sessions(.:format)                          sessions#create
     new_session GET    /sessions/new(.:format)                      sessions#new
         session DELETE /sessions/:id(.:format)                      sessions#destroy
    client_notes GET    /clients/:client_id/notes(.:format)          notes#index
                 POST   /clients/:client_id/notes(.:format)          notes#create
 new_client_note GET    /clients/:client_id/notes/new(.:format)      notes#new
edit_client_note GET    /clients/:client_id/notes/:id/edit(.:format) notes#edit
     client_note GET    /clients/:client_id/notes/:id(.:format)      notes#show
                 PUT    /clients/:client_id/notes/:id(.:format)      notes#update
                 DELETE /clients/:client_id/notes/:id(.:format)      notes#destroy
         clients GET    /clients(.:format)                           clients#index
                 POST   /clients(.:format)                           clients#create
      new_client GET    /clients/new(.:format)                       clients#new
     edit_client GET    /clients/:id/edit(.:format)                  clients#edit
          client GET    /clients/:id(.:format)                       clients#show
                 PUT    /clients/:id(.:format)                       clients#update
                 DELETE /clients/:id(.:format)                       clients#destroy
            root        /                                            clients#index
          signup        /signup(.:format)                            users#new
          signin        /signin(.:format)                            sessions#new
         signout DELETE /signout(.:format)                           sessions#destroy

和:

resources :users 
resources :sessions, only: [:new, :create, :destroy]
resources :clients do
  resources :notes
end

root to: 'clients#index'

match '/signup',  to: 'users#new'
match '/signin',  to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete

附加信息:

将行更改为<%= client_note_path(client, @notes.first) %>结果:

No route matches {:action=>"show", :controller=>"notes", :client_id=>#<Client id: 6, 
company_name: "John and Sons Roofing", contact_name: "John Wonder", phone_number: "1-    
555-283-9999", email_address: "bob@example.com", street_address: "13 Oak Street", city:
"Oakville", state: "Iowa", zip: "53457", image_location: "http://image-
location.com/image/john.jpg", created_at: "2012-06-11 16:18:16", updated_at: "2012-06-11    
16:18:16", url: "www.johnroofing.com">, :id=>nil}

这给了我一个 nil 的 ID。但是,我有<%= @notes.first.blank? ? "No Notes" : @notes.first.content%>并且返回了注释内容,所以我不明白为什么它不起作用。另外,让我们用 id 试试,只是为了好玩!<%= client_note_path(client.id, @notes.first.id) %>

现在我们得到:Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id。查看数据库,客户的便笺 ID 是 9 和 5。

4

2 回答 2

2

我相信您的问题是您将对象作为单个参数放入数组中,而不仅仅是传递两个参数。正确的方法是

client_note_path(client, @notes.first)

请注意,您可以传入对象本身(如图所示),也可以传入其中一个或两个的对象的 id。

于 2012-06-11T22:50:04.477 回答
1

client_note_path方法需要两个,count 'em,两个参数:

client_note_path(client, note)

一个Client对象和一个Note对象。或者一个 id 代表一个对象的一个Client​​和另一个。Note

你遇到的问题是因为你没有告诉它你想要什么音符,或者你使用了一个数组。通过使用数组,它只会接收一个参数。

于 2012-06-11T22:50:24.463 回答