# Simplest Collection Select Tag Not Working

**URL:** <https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300>\
**Category:** Rails\
**Created:** [June 2, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300 "2008-06-02T19:27:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 2, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300/1 "2008-06-02T19:27:33Z")

</div>

Ok I just added a collection\_select tag to the form and attempted to  
save it but the value is not getting saved. This is what my mvc looks  
like :

V (abbreviated edit.html):

\<% form\_for :user, :url =\> user\_url(@user), :html =\> { :method =\> :put } do |f| %\> \<% end %\> 

| Email: | \<%= f.text\_field :email %\> |
| Customers: | \<%= f.collection\_select(:customer\_id,@customer\_accounts,:id, :name, :include\_blank=\>true) %\> |
|   
\<%= image\_submit\_tag('Save.png') %\> |

M (abbreviated user.rb):  
class User \< ActiveRecord::Base  
validates\_presence\_of :email  
belongs\_to :customer  
end

C (abbreviated users\_controller.rb):  
def edit  
@customer\_accounts = Customer.find(:all)  
@user = current\_user  
end

def update  
@user = User.find(current\_user)  
@customer\_accounts = Customer.find(:all)  
@profile = @user.user\_profile  
if @user.update\_attributes(params[:user])  
logger.debug("\>\>\>\> user.customerid : #{@user.customer\_id}")  
if @profile.update\_attributes(params[:user][:profile\_attributes])  
flash[:notice] = “User updated”  
redirect\_to :action =\> ‘show’, :id =\> current\_user  
else  
render :action =\> ‘edit’  
end  
else  
render :action =\> ‘edit’  
end  
end

–

I tried to output customer\_id in the controller and its a blank! When I  
look at the html source of the page in firebug I see that the accounts  
field seems to be properly generated:

Army Navy Marines Air Force

Any help would be great.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 3:40am UTC](https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300/2 "2008-06-03T03:40:02Z")

</div>

Is it possible that params[:user][‘customer\_id’] is coming through from  
the form as a string and you need to to\_i it before the  
update\_attributes since customer\_id is an integer?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300/3 "2008-06-03T14:31:06Z")

</div>

Cayce B. wrote:

> Is it possible that params[:user][‘customer\_id’] is coming through from  
> the form as a string and you need to to\_i it before the  
> update\_attributes since customer\_id is an integer?

I didnt try that but I figured out the error. It was with the controller  
code; I needed to set the user.customer to @customer and then save that  
user .It was just the addition of these two lines :

```
  @user.customer = @customer
  @user.save!

```

Following is the over all code :

def update  
@user = User.find(current\_user)  
@profile = @user.user\_profile  
logger.debug("\>\>\>\> customer : #{params[:user][:customer\_id]}")  
@customer= Customer.find(params[:user][:customer\_id])  
logger.debug("\>\>\>\> customer : #{@customer.name}")  
if @user.update\_attributes(params[:user])  
logger.debug("\>\>\>\> user.customerid : #{@user.customer\_id}")  
@user.customer = @customer  
logger.debug("\>\>\>\> user.customerid : #{@user.customer\_id}")  
@user.save!  
if @profile.update\_attributes(params[:user][:profile\_attributes])  
flash[:notice] = “User updated”  
redirect\_to :action =\> ‘show’, :id =\> current\_user  
else  
render :action =\> ‘edit’  
end  
else  
render :action =\> ‘edit’  
end  
end

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/simplest-collection-select-tag-not-working/139300/4 "2008-06-03T16:34:29Z")

</div>

That certainly is a fix - but note you are making what should be an  
unnecessary database call with the .find method on Customer. Since you  
already have the customer\_id you should be able to just store it and  
move on, without having to go look up the customer again in the  
database. Although, doing it your way does provide some protection  
against rogue requests coming in from somewhere other than your form.  
But, you’ll also want to make sure you prepare for the possibility that  
the .find call doesn’t return anything and catch that exception.

Your solution even more makes me think it was a string\_to\_integer issue,  
since ActiveRecord .find works fine with a string “1” or an integer.

But, no worries - you have a fix - good work!

Ather S. wrote:

> Cayce B. wrote:
> 
> > Is it possible that params[:user][‘customer\_id’] is coming through from  
> > the form as a string and you need to to\_i it before the  
> > update\_attributes since customer\_id is an integer?
> 
> I didnt try that but I figured out the error. It was with the controller  
> code; I needed to set the user.customer to @customer and then save that  
> user .It was just the addition of these two lines :
> 
> ```
> @user.customer = @customer
> @user.save!
> 
> ```
> 
> Following is the over all code :
> 
> def update  
> @user = User.find(current\_user)  
> @profile = @user.user\_profile  
> logger.debug("\>\>\>\> customer : #{params[:user][:customer\_id]}")  
> @customer= Customer.find(params[:user][:customer\_id])  
> logger.debug("\>\>\>\> customer : #{@customer.name}")  
> if @user.update\_attributes(params[:user])  
> logger.debug("\>\>\>\> user.customerid : #{@user.customer\_id}")  
> @user.customer = @customer  
> logger.debug("\>\>\>\> user.customerid : #{@user.customer\_id}")  
> @user.save!  
> if @profile.update\_attributes(params[:user][:profile\_attributes])  
> flash[:notice] = “User updated”  
> redirect\_to :action =\> ‘show’, :id =\> current\_user  
> else  
> render :action =\> ‘edit’  
> end  
> else  
> render :action =\> ‘edit’  
> end  
> end
> 
> Thanks for the help!
