need to print "selected=selected" when editing a form

I have looked at this gropu and this page http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000506

...but I am unable to figure this out.

Here are my tables (short version) People   id   fname   lname   state_id States   id   name

I have the _form.rhtml working in a basic sense: <label for="person_state_id">State</label>: <%= select("person", "state_id", State.find(:all).collect {|s| [s.name, s.id]} ) %>

It works fine when I am creating a new record. I select the state I want and the ID goes into the state_id field. The problem is when I go to edit the page it defaults to "Alabama," the first record. I need it to select the correct state ID. I have played with it a lot and basically I need it to print this: selected="selected" on the correct state.

Suggestions?

thanks

Your controller needs to load the current row into an instance variable called @person. The selected state will be taken from @person.state_id

Show us your controller code.

def edit     @person = Person.find(params[:id])     @interests = Interest.find(:all)     @ministries = Ministry.find(:all)     @phones = Phone.find(:all)     @states = State.find(:all)   end

  def update     @person = Person.find(params[:id])     @person.interests = Interest.find(@params[:interest_ids]) if @params[:interest_ids]     @person.ministries = Ministry.find(@params[:ministry_ids]) if @params[:ministry_ids]

    if @person.update_attributes(params[:person])       flash[:notice] = 'Person was successfully updated.'       redirect_to :action => 'show', :id => @person     else       render :action => 'edit'     end   end

thanks

if you :selected option to select helper it will work, some thing like this,

<label for="person_state_id">State</label>: <%= select("person", "state_id", State.find(:all).collect {|s| [s.name, s.id]}, {:selected =>@person.state_id} ) %>

I get the same problem. by default it has "alamaba" showing up and nothing is "selected=selected" in the html code.

hmmmm....

hey Aaron,

<label for="person_state_id">State</label>: <%= select("person", "state_id", State.find(:all).collect {|s| [s.name, s.id]}, {:selected =>@person.state_id} ) %>

even after trying above code, are you facing same problem? why because, according to rails document if you pass :selected options surely it should work.

the 4th argument to select..

<%= select("person", "state_id", State.find(:all).collect {|s| [s.name, s.id]}, {:selected =>@person.state_id} ) %>

I get this: compile error script/../config/../app/views/people/_form.rhtml:94: syntax error, unexpected kEND, expecting ')'

You are missing a ')' somewhere above the line with the 'end' (i.e., line 94) and the interpreter knows that 'end' isn't valid at that point so that's where it complains.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com