2013-03-02 61 views
1

當創建新的插圖對象時,我希望顯示現有novel_ids(主鍵)的可選列表。也就是說,插圖必須添加到現有的小說中。我無法找到適當的語法來執行此操作。這些協會經過測試和適當工作。將來我希望從小說標題列表中進行選擇,但將novel_id分配給幕後插圖。謝謝。Rails - 在新對象窗體上,如何顯示關聯對象列表ids

這是我到目前爲止,我已經嘗試了許多不同風格的這種說法沒有運氣:

<div class="field"> 
    <%= f.label :novel_id%><br /> 
    <%= f.collection_select(:novel_id, Novel.all, :id, :prompt => true) %> 
</div> 
+0

A輪托架丟失。使用這個,'<%= f.select(:novel_id,options_from_collection_for_select(Novel.all,'id','name'))%>' – 2013-03-02 17:33:07

回答

1

使用此...

<%= f.select(:novel_id, options_from_collection_for_select(Novel.all, 'id', 'name')) %> 
+0

你的解決方案已經擺脫了我原來的錯誤,但現在我得到以下錯誤:'/home/joe/Documents/workspace/archive/app/views/illustrations/_form.html.erb:49:語法錯誤,意想不到的keyword_end,期待')' '); end ^ /home/joe/Documents/workspace/archive/app/views/illustrations/_form.html.erb:51:syntax error,unexpected keyword_ensure,expected')' /home/joe/Documents/workspace/archive /app/views/illustrations/_form.html.erb:53:語法錯誤,意外的keyword_end,期待')'' – 2013-03-02 17:23:10

+0

抱歉,缺少一個圓括號。現在檢查我的答案。 – 2013-03-02 17:27:41

+0

我已經實現了這個解決方案,它可以按照需要工作。如果你能提供幫助,我想更進一步。我將列出的幾本書有多個版本。數據模型將這些作爲Novel類的單獨實例。有沒有辦法顯示小說的名稱和版本以供選擇,並將新的插圖與正確的小說版組合相關聯? – 2013-03-03 13:45:40