# Creating a simple drop-down list

**URL:** <https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490>\
**Category:** Rails\
**Created:** [May 20, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490 "2007-05-20T22:32:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 20, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/1 "2007-05-20T22:32:20Z")

</div>

Hey folks,

I’m having major issues trying to complete a simple task.

Goal:  
Create a drop-down list and populate it with states. When the user  
fills out the form and saves, the state selected should be saved with  
the record.

Problem:  
When trying to save the record I get the following error:

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

And it points to this line in my .rthml:

\<%= select\_tag ‘state’, options\_for\_select(@states) %\>

And here is my controller code in the ‘new’ method:

def new  
@client = Client.new  
@states = %w{-select- AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN  
IA KS KY  
LA ME MH MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND  
OH OK  
OR PA RI SC SD TN TX UT VT VI VA WA WV WI WY}  
end

The list populates fine with states, but bombs when I try to save.  
Please help.

Thanks.

---

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 20, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/2 "2007-05-20T22:36:20Z")

</div>

longint wrote:

> Hey folks,
> 
> I’m having major issues trying to complete a simple task.
> 
> Goal:  
> Create a drop-down list and populate it with states. When the user  
> fills out the form and saves, the state selected should be saved with  
> the record.

snip…

and the save code looks like?

---

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 20, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/3 "2007-05-20T23:30:25Z")

</div>

in your model, is state just a string?

---

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 21, 2007, 1:01am UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/4 "2007-05-21T01:01:34Z")

</div>

Yeah, state is a string in my model.

---

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 20, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/5 "2007-05-20T23:12:23Z")

</div>

I had created a static scaffold, so this is what Rails’ produced:

def create  
@client = Client.new(params[:client])  
if @client.save  
flash[:notice] = ‘Client was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

---

<div class="post-metadata">

**Author:** ![longint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e7dd6/32.png) [@longint](https://www.ruby-forum.com/u/longint)\
**Post date:** [May 21, 2007, 1:23am UTC](https://www.ruby-forum.com/t/creating-a-simple-drop-down-list/127490/6 "2007-05-21T01:23:35Z")

</div>

your html from the select is just state period. not state that is part  
of client.

this:

\<%= select\_tag ‘state’, options\_for\_select(@states) %\>

just creates

... options ...

for that scaffold code to work you want

... options ...

off the top of my head, you could use select…

\<%= select ‘client’, ‘state’, @states.collect{ |s| [s, s] } %\>

but that shouldnt be were that nil inject error is coming from.  
are you sure the error is in your create method and not in either  
the redirect on the on the re-rendering?
