Nil object error - not under console

Hi there,

I’m running my app using rails 2.0.2.

When running the create method in my controller, I get the following
error:


You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

app/controllers/routes_controller.rb:39:in `create’

Parameters:

{“commit”=>“Create”,
“route”=>{“name”=>“testroute”},
“user_ids”=>[“1”,
“4”],
“heading_id”=>“0”


Here’s line 39 in the controller:

@route.users = User.find(@params[:user_ids]) if @params[:user_ids]

I don’t understand what’s going on here. The following works when I run
it in the console:

User.find([1,4])

I’m at a loss as to why this doesn’t work.

Can anyone help me here?

Thanks!

On Jun 30, 2008, at 9:24 PM, Jason F. wrote:

You might have expected an instance of ActiveRecord::Base.
“heading_id”=>“0”


Here’s line 39 in the controller:

@route.users = User.find(@params[:user_ids]) if @params[:user_ids]

Should be @route.users = User.find(params[:user_ids]) if
params[:user_ids]

The current code is trying to use an instance variable name params

Best.
Mike

Should be @route.users = User.find(params[:user_ids]) if
params[:user_ids]

Well, that fixed it. The previous code I had did work, maybe when I
upgraded to rails 2.0 it stopped? This method doesn’t get run much and
could have gone unnoticed.

Thanks so much for your help.

Jason

You’re welcome Jason. You can check out this post for more detail on
@params vs. params
http://weblog.rubyonrails.org/2006/4/25/use-params-not-params

Best.
Mike

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs