2016-04-15 49 views
-3

我試圖在Rails 4中創建應用程序。Rails 4 - 鏈接表達式的形式

我有一個配置文件模型和一個組織模型。組織有一個名爲'title'的屬性。

協會是:

Profile belongs_to organisation 

Organisations has_many profiles 

我試圖找出如何寫我的個人資料頁面顯示該組織展示頁面的鏈接。

我覺得這是正確的:

<%= link_to @profile.organisation.try(:title).upcase, organisation_path(@profile.organisation.id) %> 

然而,當我嘗試它,我得到這個錯誤:

undefined method `id' for nil:NilClass 

我可以在控制檯的profile.organisation ID是看不是零 - 它有價值。

任何人都可以看到我做錯了什麼嗎?

耙路線 - 爲組織:

organisations GET  /organisations(.:format)         organisations#index 
          POST  /organisations(.:format)         organisations#create 
      new_organisation GET  /organisations/new(.:format)        organisations#new 
      edit_organisation GET  /organisations/:id/edit(.:format)       organisations#edit 
       organisation GET  /organisations/:id(.:format)        organisations#show 
          PATCH  /organisations/:id(.:format)        organisations#update 
          PUT  /organisations/:id(.:format)        organisations#update 
          DELETE /organisations/:id(.:format)        organisations#destroy 
+0

試試這個'@ profile.organisation'而不是'@ profile.organisation.id' – uzaif

+0

如果我從該行的末尾刪除了id,我得到這個錯誤:No route matches {:action =>「show」 ,:controller =>「organizations」,:id => nil}缺少必需的鍵:[:id] – Mel

+0

向我顯示您的'rake routes'和model association .. – uzaif

回答

0

其實-the辦法我只好代碼是正確的。我不得不重置我的分貝,並重新開始。奇怪的事情導致數據庫連接出錯。如果其他人正在尋找鏈接,我原來的做法是正確的。