Forum: Ruby on Rails Application.rb params[]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daf46a55e32d22921891cf83c5d872b9?d=identicon&s=25 Spectre013 (Guest)
on 2006-01-12 22:40
(Received via mailing list)
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
C5f067a8646548d396773eb85d05a3dc?d=identicon&s=25 Mark Farver (Guest)
on 2006-01-12 22:49
(Received via mailing list)
Spectre013 wrote:

>
> if params[:day]
>
I have no idea if it is correct, but I always check using

if @params.key?("day")

Mark Farver
Daf46a55e32d22921891cf83c5d872b9?d=identicon&s=25 Spectre013 (Guest)
on 2006-01-12 23:01
(Received via mailing list)
On 1/12/06, Mark Farver <mfarver@ticom.com> wrote:
> >
> >Mark Farver




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??
Daf46a55e32d22921891cf83c5d872b9?d=identicon&s=25 Spectre013 (Guest)
on 2006-01-12 23:44
(Received via mailing list)
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
81c55e79a08ded8b4fa0e3beec9b9fed?d=identicon&s=25 Daniel Wright (Guest)
on 2006-01-12 23:47
(Received via mailing list)
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
01d7a451018ac15518f425078ef00c40?d=identicon&s=25 Phillip Hutchings (Guest)
on 2006-01-13 00:02
(Received via mailing list)
On 13/01/2006, at 11:45 AM, Daniel Wright 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 Hutchings
phillip.hutchings@sitharus.com
81c55e79a08ded8b4fa0e3beec9b9fed?d=identicon&s=25 Daniel Wright (Guest)
on 2006-01-13 00:20
(Received via mailing list)
Phillip Hutchings 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
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-01-13 00:45
(Received via mailing list)
On 1/12/06, Phillip Hutchings <sitharus-rails@sitharus.com> 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
http://www.ruby-doc.org/core/classes/Hash.html#M000650.
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-13 00:46
> 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
81c55e79a08ded8b4fa0e3beec9b9fed?d=identicon&s=25 Daniel Wright (Guest)
on 2006-01-13 01:06
(Received via mailing list)
Kevin Olbrich 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-13 01:11
Daniel Wright wrote:
> Kevin Olbrich 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
Daf46a55e32d22921891cf83c5d872b9?d=identicon&s=25 Spectre013 (Guest)
on 2006-01-13 01:18
(Received via mailing list)
On 1/12/06, Daniel Wright <dw@wonderwave.net> 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-13 01:21
Spectre013 wrote:
> On 1/12/06, Daniel Wright <dw@wonderwave.net> 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
81c55e79a08ded8b4fa0e3beec9b9fed?d=identicon&s=25 Daniel Wright (Guest)
on 2006-01-13 01:27
(Received via mailing list)
Thanks Kevin...
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-13 01:27
(Received via mailing list)
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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
This topic is locked and can not be replied to.