# Use an array in a controller to populate a select box

**URL:** <https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590>\
**Category:** Rails\
**Created:** [July 31, 2008, 9:55am UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590 "2008-07-31T09:55:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 9:55am UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/1 "2008-07-31T09:55:27Z")

</div>

I saw a post for a select tag, in the options\_for\_select([[“value 1”,  
“1”], [“value 2”, “2”], “3”, “4”],[“1”, “3”]) works fine.

If I do @test\_select = [[“value 1”, “1”],[“value 2”, “2”], “3”, “4”],  
[“1”, “3”] in the controller

I try to do options\_for\_select(@test\_select) in the place I get a NIL  
value. Any reason why the array is not getting to the view??

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 10:07am UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/2 "2008-07-31T10:07:45Z")

</div>

the array should consist of two element arrays,  
one element is the key, the other the value.  
Don’t see, what you want to do with this strange  
mix of values and arrays here.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/3 "2008-07-31T14:46:32Z")

</div>

On 30 Jul 2008, at 19:29, Me wrote:

> I saw a post for a select tag, in the options\_for\_select([[“value 1”,  
> “1”], [“value 2”, “2”], “3”, “4”],[“1”, “3”]) works fine.
> 
> If I do @test\_select = [[“value 1”, “1”],[“value 2”, “2”], “3”, “4”],  
> [“1”, “3”] in the controller
> 
> I try to do options\_for\_select(@test\_select) in the place I get a NIL  
> value. Any reason why the array is not getting to the view??

You’re probably not quite doing what you think you are. Post some code.

As a random shot in the dark, if you are calling render explicitly  
then you must set @test\_select before you do this.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/4 "2008-07-31T19:15:37Z")

</div>

In the controller I have @test\_select = [[‘New Install’,‘1’],[‘NIU  
LOOP’,‘2’],[‘CSU LOOP’,‘3’],[‘NIU\_CSU LOOP’,‘4’],[‘Maintenance’,‘5’]]

In the view I have \<%= select(‘test\_method’, ‘id’, @test\_select,  
{:prompt =\> ‘Select Test Method’ } )%\>

Why is it telling me there is a NIL value where the variable  
@test\_select is?

I am not sure how to get it to give me a select list.

On Jul 31, 7:45 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/5 "2008-07-31T19:17:22Z")

</div>

On 31 Jul 2008, at 18:14, Me wrote:

> In the controller I have @test\_select = [[‘New Install’,‘1’],[‘NIU  
> LOOP’,‘2’],[‘CSU LOOP’,‘3’],[‘NIU\_CSU LOOP’,‘4’],[‘Maintenance’,‘5’]]
> 
> In the view I have \<%= select(‘test\_method’, ‘id’, @test\_select,  
> {:prompt =\> ‘Select Test Method’ } )%\>
> 
> Why is it telling me there is a NIL value where the variable  
> @test\_select is?

post all your code, not just the snippets. eg what line is raising an  
exception and what is the stack trace?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/6 "2008-07-31T19:23:13Z")

</div>

This is the View error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.inject

Extracted source (around line #13):

10:  
11:

12:

T-1 #1 \<%=  
text\_field :circuit1, :name, :size =\> ‘15’, :value =\> “Circuit  
ID”, :onfocus =\> “if (this.value == ‘Circuit ID’) {this.value =  
‘’;}”,:onblur =\> “if (this.value == ‘’) {this.value = ‘Circuit ID’;}” -  
%\>  
13: \<%= select(‘test\_method’, ‘id’, @test\_select, {:prompt =\>  
‘Select Test Method’ } )%\>  
14: \<%= text\_field :time1, :time, :size =\> ‘5’, :value =\>  
“Time”, :onfocus =\> “if (this.value == ‘Time’) {this.value =  
‘’;}”,:onblur =\> “if (this.value == ‘’) {this.value = ‘Time’;}” -%\>  
15: \<%= submit\_to\_remote ‘commit1’, “Test Circuit”,:onclick =\> “new  
Element.show(‘save’)”,  
16: :url =\> { :action =\>  
‘diagnose\_circuit’ },

RAILS\_ROOT: F:/BACKUP/Backup/Web\_apps\_svn\_working/CircuitDiagnosis  
Application Trace | Framework Trace | Full Trace

On Jul 31, 12:16 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/7 "2008-07-31T19:29:39Z")

</div>

On 31 Jul 2008, at 18:22, Me wrote:

> This is the View error:
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.inject

stack trace sure would be useful…  
What’s in the controller ?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 31, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/use-an-array-in-a-controller-to-populate-a-select-box/144590/8 "2008-07-31T19:37:51Z")

</div>

Doh. Fixed it. Had the stinking Instance var in the wrong place in  
the controller.

On Jul 31, 12:28 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
