2011-04-19 112 views
0

試圖在Customer和Contact模型之間建立非常基本的關聯。導軌錯誤關聯has_many鏈接

Customer has_many :contacts 
Contact belongs_to :customer 
User has_many :customers 

路線

resources :customers do 
     resources :contacts 
    end 

我不想/觸點可訪問

當我在我的意見

new_customer_contacts_path 

我有一個錯誤添加。但是如果我有

new_customer_contact_path(contact) 

它的作品的鏈接聯繫#顯示是錯誤的 - >將其引導到客戶/ 7 /聯繫人/ 2,它應該是客戶/ 2 /聯繫人/ 7

任何理念?

+0

編輯您的問題,如果您需要更多的澄清。不要發佈答案。此外,您可能會想要合併您的帳戶。 – dandan78 2011-04-21 08:16:39

回答

0

你必須告訴客戶,他們的聯繫人belongs_to!

類似如下:

# Customer.first and Contact.first can be exchanged to instances 
# of Customer or Contact! 

new_customer_contacts_path(Customer.first) 
edit_customer_contact_path(Customer.first, Contact.first) 
customer_contacts_path(Customer.first) 
+0

不是我的觀點。我想訪問客戶/ ID /聯繫人/ ID而不是第一個客戶。再加上新的行動工作正常,爲什麼不顯示或編輯? – Gaelle 2011-04-19 15:54:41

+0

'Customer.first'只是一個例子!如果不指定父對象,Rails不知道如何完成嵌套的路由!嘗試'edit_customer_contact_path(customer,contact)',其中customer和contact是'Customer'和'Contact'的實例,你會看到我在說什麼! – 2011-04-19 15:59:48

0
new_customer_contact_path(contact)

這是不對的。你應該通過顧客而不是聯繫。

如果要顯示客戶的聯繫人,應使用customer_contact_path(customer,contact)。

僅供參考,請http://guides.rubyonrails.org/routing.html和搜索「創建路徑和URL從Objects的

0

不同,需要通過在他們的路線列出的順序中的對象(或者至少它們的ID)嵌套的路線。在'新'的情況下,你只需要傳遞父對象的id,因爲沒有新的嵌套對象的id。

new_customer_contact_path(customer)