我需要在 html.erb ruby 中创建动态 select
I need to create a dynamic select in an html.erb ruby
早上好。我是 Ruby 的新手,遇到了一个小问题。
在表单中,我想选择类别。类别可以由用户插入,因此选择必须是动态的。
我试过这样:
<div class="input-field">
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
</div>
有效,但在 chrome 控制台上,看起来像这样:
<div class="input-field">
<select class="multiple"></select>
<option value="2">Pastasciutta</option>
<option value="3">Vegetariano</option>
</div>
类别 ID 已正确用作值,列表已正确显示。但是,如您所见,"option" 不在 "select" 标签内。你能解释一下为什么吗?我该如何解决这个问题?
该列表仅显示为列表,但我想标记和取消标记 x 个类别。
我的版本Ruby -> 2.3.1
----编辑----
_form.html.erb
<%= form_for(recipe) do |f| %>
<% if recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% recipe.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :time %>
<%= f.number_field :time %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :recipe %>
<%= f.text_area :recipe %>
</div>
<div class="input-field">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这就是呈现的内容:
行:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
已经在呈现元素的 <select>
标记部分,您通过将上面的行放在另一个 <select>
中来复制此元素。改变这个:
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
为此:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
最后两个参数是 select 标签的 options
和 html_options
。参见:http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
早上好。我是 Ruby 的新手,遇到了一个小问题。
在表单中,我想选择类别。类别可以由用户插入,因此选择必须是动态的。
我试过这样:
<div class="input-field">
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
</div>
有效,但在 chrome 控制台上,看起来像这样:
<div class="input-field">
<select class="multiple"></select>
<option value="2">Pastasciutta</option>
<option value="3">Vegetariano</option>
</div>
类别 ID 已正确用作值,列表已正确显示。但是,如您所见,"option" 不在 "select" 标签内。你能解释一下为什么吗?我该如何解决这个问题? 该列表仅显示为列表,但我想标记和取消标记 x 个类别。
我的版本Ruby -> 2.3.1
----编辑----
_form.html.erb
<%= form_for(recipe) do |f| %>
<% if recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% recipe.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :time %>
<%= f.number_field :time %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :recipe %>
<%= f.text_area :recipe %>
</div>
<div class="input-field">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这就是呈现的内容:
行:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
已经在呈现元素的 <select>
标记部分,您通过将上面的行放在另一个 <select>
中来复制此元素。改变这个:
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
为此:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
最后两个参数是 select 标签的 options
和 html_options
。参见:http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select