# Need to print "selected=selected" when editing a form

**URL:** <https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698>\
**Category:** Rails\
**Created:** [September 27, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698 "2007-09-27T19:14:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [September 27, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/1 "2007-09-27T19:14:46Z")

</div>

I have looked at this gropu and this page  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000506](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:  
State: \<%= 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

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [September 27, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/2 "2007-09-27T20:05:54Z")

</div>

On 9/27/07, Aaron R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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.

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [September 27, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/3 "2007-09-27T20:26:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [September 28, 2007, 7:26am UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/4 "2007-09-28T07:26:15Z")

</div>

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

State: \<%= select(“person”,  
“state\_id”, State.find(:all).collect {|s| [s.name, s.id]}, {:selected  
=\>@person.state\_id} ) %\>

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [October 1, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/5 "2007-10-01T16:29:12Z")

</div>

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

hmmmm…

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [October 2, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/6 "2007-10-02T15:00:01Z")

</div>

hey Aaron,

State: \<%= 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} ) %\>

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [October 8, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/7 "2007-10-08T22:12:32Z")

</div>

On Oct 8, 2007, at 10:23 AM, Aaron R. wrote:

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lazer\_rash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lazer\_rash](https://www.ruby-forum.com/u/lazer_rash)\
**Post date:** [October 8, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/need-to-print-selected-selected-when-editing-a-form/115698/8 "2007-10-08T16:24:45Z")

</div>

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