Application.rb params[]

I need to check if a parameter is set so that I can build some
information
for my application, but No matter how I format my if statement in the
file "
Application.rb" it return the following error.

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.[]

Here is the line

if params[:day]

So we have a nil I know that, its why I am checking to see if it’s empty
or
not.

Here is the funny part just a little lower on the page we have

Parameters: {“month”=>“04”, “day”=>“04”, “year”=>“2002”,
“section”=>“metro”}

so params[:day] should be available.

I have also tried just “params” and “@params” and several other ways all
with the same result. Have also tried == and != with the if all
resulting in
a nil error.

is there some thing that would cause the params hash to not be available
in
the application.rb file?

any help would be appreciated.
Brian

Spectre013 wrote:

if params[:day]

I have no idea if it is correct, but I always check using

if @params.key?(“day”)

Mark F.

Spectre013 wrote:

so params[:day] should be available.
Brian

I am sure there is a shorter way of doing this but

@day_param = @params[‘day’]
@day = @day_param.inspect
if @day == “nil”

Dan

On 1/12/06, Mark F. [email protected] wrote:

Mark F.

Tried that and ended up with

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.key?

which is looking like @params is empty. would there be some reason the
params hash is not set or available to the application.rb??

Just as an answer to this if any one has a similar problem it seem that
the
@prams is not available to the initialize method. I moved my code out to
the
application_helper.rb and was able to get it workin ok.

Hope this helps

Brian

On 13/01/2006, at 11:45 AM, Daniel W. wrote:

@day_param = @params[‘day’]
@day = @day_param.inspect
if @day == “nil”

A hash always returns nil for an unknown key. All objects have a nil?
method. Try this:

if @params[‘day’].nil?


Phillip H.
[email protected]

Phillip H. wrote:

if @params[‘day’].nil?
http://lists.rubyonrails.org/mailman/listinfo/rails

Hi Phillip,
Not meaning to hijack this thread, I figured that my problem/solution
was relevant to the OP’s.

How would I incorporate your example into my existing code?
specifically the method under the else statement?

def index
@category = @params[‘category_id’]
@categories = @category.inspect
if @categories == “nil”
@items = Item.find_all
else
@items = Item.find_all(“category_id=”+@category)
end
end

Dan

On 1/12/06, Phillip H. [email protected] wrote:

A hash always returns nil for an unknown key.

Off topic, but just FYI, nil is only the default value for an unknown
key:

irb(main):006:0> foo = Hash.new(:foo)
=> {}
irb(main):007:0> foo[:bar]
=> :foo

Hash.new can even take a block, which gets called when an unknown key
is accessed. See
class Hash - RDoc Documentation.

def index
@category = @params[‘category_id’]
@categories = @category.inspect
if @categories == “nil”
@items = Item.find_all
else
@items = Item.find_all(“category_id=”+@category)
end
end

Dan

def index
@category = params[‘category_id’]
@items = Item.find_all unless @category
@items = Item.find_all_by_category(@category) if @category
end

The params hash may be empty, but the most likely explanation is that
params[:day] != params[“day”]

_Kevin

Daniel W. wrote:

Kevin O. wrote:

With this I get the error:

Undefined method `find_all_by_category’ for Item:Class

I changed the third line back to:

Item.find_all(“category_id=”+@category)

which made it work, and I shortened the method by 3 lines of code.

Thanks,

Dan

If you are going to use that form, use this instead to avoid SQL
injection…

Item.find_all(:conditions=>[“category = ?”, @category])

I’m not sure why the dynamic finder doesn’t work in your case…

_Kevin

On 1/12/06, Daniel W. [email protected] wrote:

    end

which made it work, and I shortened the method by 3 lines of code.

Thanks,

Dan

I have it woking no problem with the inital call to the method not being
inside of the initalize method.

EXAMPLE:

class StoriesController < ApplicationController
def intialize
if @params[:id]
…more code here
end
end

will throw the nil error, I am not 100% sure but it seems to me that the
params hash is not set up when the initialize method is called.

I have since moved it to a helper and it works just fine. In fact that
was a
better place for it i think.

If some on knows or finds out about the params and the initialize method
it
would be nice to find out.

I will keep looking.

Brian

Kevin O. wrote:

With this I get the error:

Undefined method `find_all_by_category’ for Item:Class

I changed the third line back to:

Item.find_all(“category_id=”+@category)

which made it work, and I shortened the method by 3 lines of code.

Thanks,

Dan

Spectre013 wrote:

On 1/12/06, Daniel W. [email protected] wrote:
will throw the nil error, I am not 100% sure but it seems to me that the
params hash is not set up when the initialize method is called.

This is probably correct. The initialize method is your object
constructor.

I bet if you did somehting like this it would work…

def initialize
super
if params[:id] …

end

That might let the base classes set up the hash before you tried to use
it.

_Kevin

Thanks Kevin…

On Jan 12, 2006, at 4:14 PM, Spectre013 wrote:

       @items = Item.find_all("category_id="+@category)

@items = Item.find_all_by_category(@category) if @category
Item.find_all(“category_id=”+@category)
being inside of the initalize method.
will throw the nil error, I am not 100% sure but it seems to me

Brian

Brian-

The initialize method of a controller is outside of the request

response cycle so it doesn’t know anything about params. You can get
around this by using filters. Set it up like this:

class StoriesController < ApplicationController

before_filter :do_stuff

def do_stuff
# do stuff with params…
end
end

Cheers-

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
[email protected]