Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby-从数据库填充时的文本解析问题_Ruby - Fatal编程技术网

ruby-从数据库填充时的文本解析问题

ruby-从数据库填充时的文本解析问题,ruby,Ruby,尝试使用数据库中的值在ruby中创建简单下拉列表-如下所示: <% ingredientArray = Ingredient.all.map { |ingredient| [ingredient.name, ingredient.id] } %> <div class="field"> <%= select_tag(:ingredient_id, ingredientArray) %><br/> </div> 我还收到了一张

尝试使用数据库中的值在ruby中创建简单下拉列表-如下所示:

<% ingredientArray = Ingredient.all.map { |ingredient| [ingredient.name, ingredient.id] } %>
<div class="field">
  <%= select_tag(:ingredient_id, ingredientArray) %><br/>
</div>


我还收到了一张empy卡。
这是生成的html

<div class="field">
   <select id="ingredient_id" name="ingredient_id">[[&quot;Paprika&quot;, 5], [&quot;Cinnamon&quot;, 8], [&quot;Salt&quot;, 9], [&quot;Pepper&quot;, 10], [&quot;water&quot;, 11]]</select><br/>
</div>

[[“红辣椒”,5],“肉桂”,8],“盐”,9],“胡椒”,10],“水”,11]

我应该把html sage放在哪里?您应该阅读有关html sage和相关方法的文档。 它的第二个参数是一个字符串,包含选择框的选项标记。 您可以在中手动生成:

select_tag "people", "<option>David</option>".html_safe
(文档中的示例)

具体来说,在您的情况下,最好的下拉方式是:

<div class="field">
    <%= select_tag("Ingredients", options_from_collection_for_select(Ingredient.all, 'id', 'name')) %>
</div>

您也可以这样使用:


(我假设您尝试将配料ID分配给的父对象是
:recipe
。请根据应用程序更改该值。)

<div class="field">
    <%= select_tag("Ingredients", options_from_collection_for_select(Ingredient.all, 'id', 'name')) %>
</div>
<%= collection_select :recipe, :ingredient_id, Ingredient.all, :id, :name, { prompt: "&ndash; Select an Ingredient &ndash;".html_safe } %>