Creating a simple drop-down list

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.

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?

in your model, is state just a string?

Yeah, state is a string in my model.

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

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?