# Select in fields\_for not selecting

**URL:** <https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739>\
**Category:** Rails\
**Created:** [January 14, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739 "2009-01-14T22:48:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [January 14, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739/1 "2009-01-14T22:48:41Z")

</div>

I have a user who has a location that location contains a country\_id.  
Location has a polymorphic interface locatable.

What works:  
The user can edit their name, country and city. This all saves to the DB  
on the update put.

The problem:  
Returning to the edit page shows the textfield’s name and city populated  
but the country select form element defaults to blank. The value for  
user.location.country\_id is saved in the DB but the form element refuses  
to show this as “selected”. I have attempted explicityly setting the  
selected :option but still, it fails to show country.

MODEL

user  
has\_one :location, :as =\> :locatable, :dependent =\> :destroy

location  
belongs\_to :country  
belongs\_to :locatable, :polymorphic =\> true

country  
has\_many :locations

VIEW  
\<% form\_for @user, :html =\> { :multipart =\> true } do |form| %\>

 \<%= form.label :name %\> \<%= form.text\_field :name %\> 

\<% fields\_for “user[location\_attributes]”,@user.location do  
|location\_fields| %\>

 \<%= location\_fields.label :country\_id %\> \<%= location\_fields.select :country\_id, Country.find(:all).map { |c| [c.name.titleize, c.id] }, { :include\_blank =\> true } %\> 

 \<%= location\_fields.label :city %\> \<%= location\_fields.text\_field :city %\> 
\<%- end -%\>

\<%= form.submit ‘Update’ %\>  
\<% end %\>

CONTROLLER

def edit  
@user = User.find(params[:id])  
end

def update  
@user = User.find(params[:id])  
@user.attributes = params[:user]  
@user.save!  
flash[:notice] = “Your profile has been updated.”  
redirect\_to url\_after\_update

rescue ActiveRecord::RecordInvalid  
render :action =\> “edit”  
end

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [January 14, 2009, 11:11pm UTC](https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739/2 "2009-01-14T23:11:47Z")

</div>

I forgot to mention.  
I adjusted the fields\_for name to “user[location]” in case  
“\_attributes” was causing the problem.

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [January 15, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739/3 "2009-01-15T15:48:12Z")

</div>

You may want to consider adding attr\_accessible for the editable  
fields in your model.

/dkm

On Jan 14, 5:11 pm, Adam madd02 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [January 15, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/select-in-fields-for-not-selecting/157739/4 "2009-01-15T19:41:51Z")

</div>

Thanks for your reply DKM,

I have location\_attributes listed in the users attr\_accessable list and  
use attr\_protected for the :locatable\_id and :locatable\_type in  
location.

Just to check that there are no attribute access issues i commented out  
attr\_accessible and attr\_protected’s in the user and location models. It  
still did not set the select.

I believe it’s soemthing to do with the select name generated from the  
fields\_for.  
  
.  
.

but im not sure what.
