Ruby on Rails - form_for collection_select选项不可见

时间:2013-08-30 11:16:11

标签: ruby-on-rails

我在form_for中使用了collection_select,但是选项没有显示在浏览器中(空选择框),但存在于调试器视图中。 (这在chrome和IE10中都会发生。)

查看:

<%= form_for(@lot) do |f| %>
<%= f.label :client_id, "Client" %>
<%= f.select :client_id, collection_select(:lot, :client_id, Client.all, :id, :org, :include_blank => "Please select") %>

渲染页面来源:

<label for="lot_client_id">Client</label>
<select id="lot_client_id" name="lot[client_id]"></select>
<option value="">Please select</option>
<option selected="selected" value="1">Client 1</option>
<option value="2">client 2</option>

控制器:

def new
  @lot = Lot.new(:client_id => 1)
end

非常感谢任何见解,谢谢,

3 个答案:

答案 0 :(得分:1)

你可以试试这个例如。

<%= f.collection_select(:category_id, Category.all,:id,:name, :required=>true) %>

答案 1 :(得分:0)

collection_select也是formHelper种类,它们同时返回select和options元素,请尝试:

<%= f.collection_select(:lot, :client_id, Client.all, :id, :org, :include_blank => "Please select") %>

代替

答案 2 :(得分:0)

您正在select helper中渲染collection_select帮助器,这是错误的。你必须这样做:

<%= f.collection_select(:client_id, Client.all, :id, :org, :include_blank => "Please select") %>