# Add value to select box

**URL:** <https://www.ruby-forum.com/t/add-value-to-select-box/151846>\
**Category:** Rails\
**Created:** [October 26, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/add-value-to-select-box/151846 "2008-10-26T22:29:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 26, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/add-value-to-select-box/151846/1 "2008-10-26T22:29:48Z")

</div>

Hey guys!

I was wondering. I’m generating a select box in my view where the user  
can pick from a number of alternatives. However, I would like the last  
option to be “Add new…” and then a box would pop up allowing the  
user to enter a new value.

Now I’ve got all the JavaScript and the Rails code to allow the user  
to enter a new option, the problem is how do I add the last  
alternative (“Add new…”) to my select box?

Take care!  
Alex

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 26, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/add-value-to-select-box/151846/2 "2008-10-26T23:27:13Z")

</div>

> Now I’ve got all the JavaScript and the Rails code to allow the user  
> to enter a new option, the problem is how do I add the last  
> alternative (“Add new…”) to my select box?

In your controller:

@options = Option.find(:all).collect {|p| [p.name, p.id] }  
@options \<\< [“Add New”, nil]

Then in your view:

\<%= select(“user”, “choice\_id”, @options, { :include\_blank =\> true })  
-%\>

This should work, because select just iterates over an array taking  
the .first and .last values from it.

Mikel

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 27, 2008, 12:57am UTC](https://www.ruby-forum.com/t/add-value-to-select-box/151846/3 "2008-10-27T00:57:17Z")

</div>

Hi Alex,

> However, I would like the last option to be “Add new…”  
> and then a box would pop up allowing the user to enter a  
> new value.
> 
> Now I’ve got all the JavaScript and the Rails code to allow  
> the user to enter a new option, the problem is how do I add  
> the last alternative (“Add new…”) to my select box?

Assuming that the action that adds the new option creates a new record  
in  
the db and that you’re rendering a partial to display the select in the  
first place (if not, refactor), just add a find to your action and  
re-render  
the select via RJS.

HTH,  
Bill
