10

There are so many results for this search on google, and it's even asked at SO - but the solutions discussed so far are not helping me. Here's the issue: I have a form_for @company |f| and I am using f.collection_select for company_status_id - but when the form loads, I want the actual company status selected if it is set. Through the debugger I know, that it's been set, yet I am getting a default value displayed there. Here's the code:

= puts @company.company_status_id
= f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value}

Here's the generated htmnl

<select id="company_company_status_id" prompt="-Select-" name="company[company_status_id]">
<option value="1">-Not Available-</option>
<option value="2">Active</option>
<option value="3">Bankrupt</option>
<option value="4">Acquired</option>
</select>

And the conditions remain the same even if I do:

f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value, :selected => :selected => @company.company_status}

Or

f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value, :selected => @company.company_status}
Saad Rehman Shah
  • 926
  • 2
  • 10
  • 28
  • Through the puts statement over there, I see the company_status_id being set, but not working correctly. I am using rails 2.3.11. [this](http://stackoverflow.com/a/1065359/878451) answer tells exactly what I want, and also what I am doing, but what doesn't work. – Saad Rehman Shah Apr 19 '12 at 10:43
  • this should work. show @company.company_status_id and ListCache.company_statuses – tokland Apr 19 '12 at 11:09
  • Can you post your form definition as its going to depend what f.object is? – Lee Irving Apr 19 '12 at 11:27
  • I meant: "it should work". I don't see anything wrong. So show some values to see what's going on. – tokland Apr 19 '12 at 11:33
  • `f.object = company` and 'ListCache.company_statuses' returns the entire table of company_stasus which have an `id` and a `name`, so I guess it will be an array of hashes. – Saad Rehman Shah Apr 19 '12 at 11:41

5 Answers5

20

This is what I finally did:

f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value, :selected => @company.company_status_id.to_i}

I read on of the answers on a similar question that collection_select automatically selects the selected value by making comparisons of what is passed with the attributes of collection. apparently there was a difference of their types, and comparing the int from CompanyStatus to the int of @company.company_status_id.to_i worked out. Though @company.company_status_id is supposed to be int as well. I can see that in the db. Anyway, it this line of code worked.

If anyone can exaplain, I will be much thankful!

Saad Rehman Shah
  • 926
  • 2
  • 10
  • 28
6

If you use collection_select helper, syntax is very simple:

<%= f.collection_select :category_id, Category.all, :id, :name,
                       prompt: true, selected: @product.category_id %>

I hope this help

Raf
  • 91
  • 1
  • 3
2
<% form_for(@company) do |f| %>
   <%= f.select(:company_status_id, ListCache.all.map {|lc| [lc.name, lc.id]} ) %>
<% end %>
TomDunning
  • 4,829
  • 1
  • 26
  • 33
1

Sometimes you just need to go to the browser address bar and press enter. Normal reloading the page clicking the refresh button doesn't help. My problem was solved that way.

zawhtut
  • 8,335
  • 5
  • 52
  • 76
0

Use select_tag instead

    <%= form_for(@product, :html => {:multipart => true}) do |f| %>


     <%= select_tag("product[category_id]", options_for_select(@categories.map { |cat| [cat.name, cat.id] })) %>

     <%end%>

Hope this help you.....

Kashiftufail
  • 10,815
  • 11
  • 45
  • 79
  • If I were to use that with the form object, f, how would it look like? Like This? `<%= f.select("model_name_object[company_status_id]", options_for_select(@categories.map { |cat| [cat.name, cat.id] })) %>` – Saad Rehman Shah Apr 19 '12 at 10:50