views:

469

answers:

3

Hey Guys,

Ive just set up some basic models using scaffold with Rails, i've edited the views so that im taking input from a textfield and a selection box. Here are some code snippets:

form_for(@town) do |f|
f.text_field :name
.
.
f.select :county_id, @counties.map{|c| [c.name, c.id]}
.
.
end

Using the validation method "validates_presence_of" for the textfield and no validation on the selection box I can enter values and successfully write to the database. When I try to test validation by not entering anything into the textfield, I get a NoMethodError with the following message

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.map

Note: there is no validation on the selection box

If I comment out the "validate_presence_of" line I can write blank data to the db with no problems. However if I write validate code for the textfield and it cause the entire form object becomes nil apparently...is Rails' validation causing this?

Any thoughts/comments are greatly appreciated. Im using Rails 2.3.4

+1  A: 

In your new method, how are you creating @counties and what are you putting into that variable? Also, without seeing your new and create methods, it is somewhat hard to tell exactly what is going on. Are you getting this error when you try to access the page above or when you try and submit the form?

For what it's worth, you do not have validations on form objects, you have validations on model attributes. As such, you need to check the params that are being passed to your controller when you submit your form and ensure that you are getting what you expect. Then match those params to the various validates_ methods that you have in your model.

Debugging this is a simple case of first checking to ensure that your form and the data in it are correct and then that the params being sent to your controller are correct. Remember, you need to check both the key and the value.

Also, use script/console and try creating a few model objects using the data that you are expecting your form to send and see what happens.

Chris Johnston
My validation code is just one line in town.rb "validates_presence_of :name". When I change the selection box to a textfield the validation performs correctly but when I change it back again it breaks. I tried taking the `@counties.map{|c| [c.name, c.id]}` code out of the view and placing it in the controller but I then get a `The error occurred while evaluating nil.inject` error. I tried using script/console as you mentioned but to no avail
gcahill
But what is @counties? Show us the code that you use to create it? Your problem is that what you have inside @counties is not what the select is expecting. I don't think it has anything to do with your validation.
Chris Johnston
+1  A: 

You haven't posted the stack trace, but I'm assuming the error is occurring when you call map on @counties.

You probably have two actions in your controller handling the form. One (either new or edit) shows the form and responds to get requests. The other (either create or update) does the creation or update of the model. This responds to post or put requests and will either redirect if successful or render the new or edit action when there are validation errors (if save fails).

I expect your new or edit action has some code that assigns to @counties. Your create or update action will also need to assign @counties when save fails so that it is available in the view.

Phil Ross
Hey guys, apologies for the ambiguity in my question, I didnt understand that `create` was calling `new.html.erb` when save failed. I verified this from the trace. I've updated create so that it assigns to @counties. Thanks Phil, I really should change my name to railsnewb9999
gcahill
+1  A: 

See this question

Your problem is symptom of the same thing.

Tony Fontenot