我正在尝试创建一个更新表格的表单。我想要一个下拉按钮,从一个名为“制造商”的表中提取信息,一旦提交表单,就将数据存储在“评论”表中。
这是我的表格:
<%= 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 %>
当你看到这一行 - <%= f.collection_select :bat_id, Manufacturer, include_blank: true %>
我相信:bat_id告诉表单从哪里发送用户的输入。 (在本例中为评论表中的:bat_id参数)
如何告诉表单从制造商表中选择下拉选项?
更新 我需要在控制器中添加任何内容吗?
答案 0 :(得分: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
字段,否则您需要适应