views:

41

answers:

2

Hi Everyone,

I have number of select lists in my rails application like this:

<li>Company<span><%= f.select :company_id, Company.all.collect {|m| [m.companyname, m.id]} %></span></li>

They all work well, except - sometimes if you go to the edit view, the select list reverts to the top item, not the item that was chosen when creating. So if you go to an edit view and then click update without actually making any changes, the lists default to the top item - even though you didn't touch them.

Is there a way around this?

Thanks,

Danny

EDIT:

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


<li>Appointed Surveyor<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => @kase.appointedsurveyor_id}  %></span></li>
    <li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => @kase.surveyorperson_id} %></span></li>

I have tried the above, but sadly it still seems to revert to the default value.

I'm stuck on this, I can't find any tutorials etc on this at all.

EDIT 2:

<li>Appointed Surveyor<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (@kase.appointedsurveyor_id rescue "")} %></span></li>
    <li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>
+2  A: 

You might be able to make sure of it with the third parameter, try the following code:

<li>Company<span><%= f.select :company_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => @your_instance_name.company_id} %></span></li>

Make sure that you replace @your_instance_name with the instance variable you use at the form_for tag.

Hopefully that helps.

SamChandra
Hi, I tried your suggestion but it didnt seem to work. I have pasted the code I tried in my question in case I made a mistake.
dannymcc
A: 

It turns out I had the fields in the database as strings not integers.

dannymcc