views:

488

answers:

2

Hey guys,

So I generate my scaffold in rails, and it creates the usual CRUD files. In my View, I copy over the form found in new.html.erb and paste it over at index.html.erb, so I can create a new record from my index. When I do that, I get the following error consistently, no matter what I do.

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

I got tired of searching all over the web for answers, and just learned it's called a whiny nil (not much help). I tried renaming my instance variables, capitalization, using global variables, etc. but it's frustrating that Rails doesn't have an error documentation library. Can anyone help?

+5  A: 

Do you have all the required objects present in your controller? Looks like it's calling something.id, but that something does not exist in your index action. Look at the whole error message - it should be saying what line is causing it, then check that line in source files for the missing variable.

Toms Mikoss
+1  A: 

In your controller, you need to add the code found in new into index (I guess it is something like @model = Model.new). Or better: Make a private method which has name expose_new or something like that, move the common code down there and add before_filter :expose_new, :only => [:index, :new].

Just a side note..

If I were you, I'd make a partial out of your form in new, and render that in both index and new (and quite possible edit if them all are equal) so you don't need to copy paste it in.

So, you'll end up with one _form.html.erb which includes the form, and in new and index, you have <%= render('form') %>

Thorbjørn Hermansen
I nested the code of my 'new' and 'create' methods into my 'index' method, but it doesn't work. Did you mean something else specifically?
Can you post the code for your index action and your index view?
mikej