user2732663
user2732663

Reputation: 833

Ruby on Rails - form_for collection_select options not visable

I'm using a collection_select in a form_for however the options are not displaying in the browser (empty select box) but are present in the debugger view. (this happens in both chrome and IE10).

View:

<%= 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") %>

Rendered page source:

<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>

Controller:

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

Any insight would be much appreciated thanks,

Upvotes: 0

Views: 176

Answers (3)

techvineet
techvineet

Reputation: 5111

You are rendering collection_select helper inside the select helper, which is wrong. You have to do it like this:

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

Upvotes: 0

Seb Ashton
Seb Ashton

Reputation: 692

collection_select is also a formHelper of sorts which returns both a select and options elements, try:

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

instead

Upvotes: 0

Sagar.Patil
Sagar.Patil

Reputation: 991

you can try this as eg.

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

Upvotes: 1

Related Questions