# Storing combo box data in the database

**URL:** <https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621>\
**Category:** Rails\
**Created:** [November 19, 2007, 10:05am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621 "2007-11-19T10:05:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 10:05am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/1 "2007-11-19T10:05:58Z")

</div>

Hi,  
I am using a combo box in a form along with some other form  
elements… The code that i have written is:

```
   <p><label for="employee_Fname">Fname</label><br/>

```

\<%= text\_field ‘employee’, ‘Fname’ %\>

Lname  
\<%= text\_field 'employee', 'Lname' %\>

Doj  
\<%= date\_select 'employee', 'DOJ' %\>

Email@id  
\<%= text\_field 'employee', 'email@Id' %\>

Service  
\<%= select\_tag 'employee', options\_for\_select(%w{None ITS RDS Corporate},"None") %\>

```
 The problem is when i submit the form data it is not storing

```

anything in the database… But when i am not using combo box its  
working fine. The controller code is…

```
def create
@employee = Employee.new(params[:employee])
if @employee.save
  flash[:notice] = 'Employee was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

Can anybody help???  
Thanks in advance…

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 10:10am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/2 "2007-11-19T10:10:04Z")

</div>

On 19 Nov 2007, at 09:05, Subhash wrote:

> Service  
> \<%= select\_tag 'employee', options\_for\_select(%w{None ITS RDS Corporate},"None") %\>
> 
> ```
> The problem is when i submit the form data it is not storing
> 
> ```
> 
> anything in the database… But when i am not using combo box its  
> working fine. The controller code is…

You’re using select instead of select\_tag, so there parameter is being  
submitted as params[:employee], clobbering the rest of the data (a  
hash in params[:employee]).  
Either use select or your select\_tag should be select\_tag  
‘employee[foo]’, … instead of what it currently is.

Fred

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 11:18am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/3 "2007-11-19T11:18:40Z")

</div>

Hi Fred,  
Thanks for your suggestion… I have made the necessary  
changes that you said…now the error is gone but still the data is  
not getting added in the database…

First I tried with select at first:

Service  
\<%= select( "employee", "service", { "None" =\> "None", "ITS" =\> "ITS", "RDS" =\> "RDS", "Corporate" =\> "Corporate"}) %\>

Next with select\_tag:

Service

\<%= select\_tag “employee[service]” , options\_for\_select(%w { None RDS  
ITS Corporate} , “None”) %\>

Looking forward to your response…

On Nov 19, 2:09 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 11:39am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/4 "2007-11-19T11:39:14Z")

</div>

On 19 Nov 2007, at 10:17, Subhash wrote:

> Hi Fred,  
> Thanks for your suggestion… I have made the necessary  
> changes that you said…now the error is gone but still the data is  
> not getting added in the database…

The logical question to ask is why is it not getting saved? If a  
validation is failing, which one? In the logs what is the data that  
rails is receiving ? etc…

Fred

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/5 "2007-11-19T12:29:12Z")

</div>

Hi Fred,  
I checked in the log file, you are right some validation  
is failing… Actually when the create method is getting fired, rails  
is generating an insert statement…in that insert statement the  
field for which I have put the combo box that name isnt there.  
When I started developing this application I used  
scaffold…the view part generated by rails did not have any combo  
box. All were text fields except the date. I replaced one text field  
with the combo box. Do you think that could be the problem??? I am  
new to this ROR and I am not getting any clue…

On Nov 19, 3:38 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/6 "2007-11-19T12:43:16Z")

</div>

On 19 Nov 2007, at 11:27, Subhash wrote:

> ```
> When I started developing this application I used
> 
> ```
> 
> scaffold…the view part generated by rails did not have any combo  
> box. All were text fields except the date. I replaced one text field  
> with the combo box. Do you think that could be the problem??? I am  
> new to this ROR and I am not getting any clue…

In the log file, what is being submitted by the browser?

Fred

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/7 "2007-11-19T13:02:00Z")

</div>

Processing EmployeesController#create (for 127.0.0.1 at 2007-10-19  
17:55:21) [POST]  
Session ID: 8aecbc2b2dd0e9ad76116ce8eed371d8  
Parameters: {“commit”=\>“Create”, “employee”=\>{“DOJ(1i)”=\>“2007”,  
“Lname”=\>“fsfdsf”, “DOJ(2i)”=\>“10”, “email@Id”=\>“dfsfs”,  
“DOJ(3i)”=\>“19”, “Fname”=\>“fsfs”}, “action”=\>“create”,  
“controller”=\>“employees”}  
[4;36;1mEmployee Columns (0.000000) [0m [0;1mSHOW FIELDS FROM  
employees [0m  
[4;35;1mSQL (0.000000) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.000000) [0m [0;1mINSERT INTO employees (`Lname`,  
`email@Id`, `DOJ`, `Fname`) VALUES(‘fsfdsf’, ‘dfsfs’, ‘2007-10-19’,  
‘fsfs’) [0m  
[4;35;1mSQL (0.000000) [0m [0mCOMMIT [0m  
Redirected to [http://localhost:3000/employees/list](http://localhost:3000/employees/list)  
Completed in 0.11000 (9 reqs/sec) | DB: 0.00000 (0%) | 302 Found  
[[http://localhost/employees/create](http://localhost/employees/create)]

On Nov 19, 4:42 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 19, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/8 "2007-11-19T12:42:05Z")

</div>

Hi,  
I forgot to give one information…The table in the database is having  
5 fields…  
Fname, Lname, Date of joining, email and service. It is the last  
field for which i am using the combo box. Is it possible to force  
rails to generate a combo box according to the requirement???

---

<div class="post-metadata">

**Author:** ![subhash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@subhash](https://www.ruby-forum.com/u/subhash)\
**Post date:** [November 20, 2007, 6:03am UTC](https://www.ruby-forum.com/t/storing-combo-box-data-in-the-database/120621/9 "2007-11-20T06:03:14Z")

</div>

On 19 Nov 2007, at 11:48, Subhash wrote:

> Session ID: 8aecbc2b2dd0e9ad76116ce8eed371d8  
> Parameters: {“commit”=\>“Create”, “employee”=\>{“DOJ(1i)”=\>“2007”,  
> “Lname”=\>“fsfdsf”, “DOJ(2i)”=\>“10”, “email@Id”=\>“dfsfs”,  
> “DOJ(3i)”=\>“19”, “Fname”=\>“fsfs”}, “action”=\>“create”,  
> “controller”=\>“employees”}

Your form isn’t submitting the service field, so it’s nothing to do  
with the database stuff.  
You probably want to look at the source for your entire field to  
figure out why this is happening.

Fred.
