2014-02-07 48 views
0

我想創建一個表格來更新表格。我想要一個下拉按鈕來從一個名爲「製造商」的表中提取信息,並且一旦表單被提交,就將數據存儲在「評論」表中。使用多個表格的導軌形式

這是我的形式:

<%= form_for(@review) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field" align= "center"> 
    <h3>Select bat</h3> 
    <%= f.collection_select :bat_id, Manufacturer, include_blank: true %> 
    <h3>What do you like about this bat?</h3> 
    <%= f.text_area :pros, placeholder: "Enter what you like..." %> 
    <h3>What do you not like about this bat?</h3> 
    <%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br> 
    </div> 
    <div align="center"> 
    <%= f.submit "Add Review", class: "btn btn-large btn-info" %> 
    </div> 
<% end %> 

當你看到這LINE-<%= f.collection_select :bat_id, Manufacturer, include_blank: true %>我相信:BAT_ID告知的形式,其中從用戶發送輸入。 (在本例中爲複查表中的:bat_id參數)

如何將表單從製造商表格中選擇下拉選項?

更新: 我需要在控制器中添加任何東西嗎?

+0

你可以試試簡單形成提供關聯選擇輸入的寶石 – nickcen

回答

0

查看文檔http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select

collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) 
# method: the field in your form's object 
# collection: the collection of active records to display as options 
# value_method: which method call in items of the collection to get the displayed value 
# text_method: which method call in items of the collection to get the id value 

所以你的情況:

<%= f.collection_select :bat_id, Manufacturer.all, :id, :name, include_blank: true %> 

鑑於Manufacturer模型具有name場,否則,你需要適應

+0

我嘗試了上面的但它讀取「NoMethodError」和「未定義的方法'映射'爲#「 – Daniel

+0

'Manufacturer.all',而不僅僅是'Manufacturer'。 – sevenseacat

+0

沒錯,謝謝。固定 – Benj