嵌套路由显示操作:它期望什么对象?

时间:2012-06-11 22:38:43

标签: ruby-on-rails ruby-on-rails-3

以下是我的佣金路线的相关行:

 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}

仍然不是我想要的。我希望能够显示一个通常可以在网址上找到的单个注释:http://localhost:3000/clients/2/notes/3/

它期望什么对象?

完成路由文件和Rake路由

           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}

这给了我一个零的身份。但是,我有<%= @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。

2 个答案:

答案 0 :(得分:2)

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

client_note_path(client, @notes.first)

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

答案 1 :(得分:1)

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

client_note_path(client, note)

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

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