Forum: Ruby on Rails testing for non-existent parameters with "if"

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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2007-03-04 08:09
Rails gives me an error when I test for a blank param like this:

        if params[:post][:id]
          @comment.commentable = Post.find(params[:post][:id])
        elsif params[:event][:id]
          @comment.commentable = Event.find(params[:event][:id])
        end

If I have params[:post][:id], things are fine.  But if I have
params[:event][:id] instead, I get this 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.[]

Rails dies when it checks for the non-existent params[:post][:id].  I
will always send either :event or :post, but never both.  How can I use
"if - elsif" to test for this without crashing?  Thanks!
Ea627ef000ec92c6cdd5a4c14075e740?d=identicon&s=25 Dan Kubb (Guest)
on 2007-03-04 08:31
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Taylor,

On 3-Mar-07, at 11:09 PM, Taylor Strait wrote:

>
> 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.[]
>
> Rails dies when it checks for the non-existent params[:post][:id].  I
> will always send either :event or :post, but never both.  How can I
> use
> "if - elsif" to test for this without crashing?  Thanks!

If the params[:post] hash is non-existent, then checking within it
to see if there's an :id value is probably what's throwing that error.

To avoid this problem you'd want to do something like:

   if params[:post] && params[:post][:id]
     @comment.commentable = Post.find(params[:post][:id])
   elsif params[:event] && params[:event][:id]
     @comment.commentable = Event.find(params[:event][:id])
   end

Or you could do something like this:

   if id = params[:post][:id] rescue nil
     @comment.commentable = Post.find(id)
   elsif id = params[:event][:id] rescue nil
     @comment.commentable = Event.find(id)
   end

- --

Thanks,

Dan
__________________________________________________________________

Dan Kubb
Autopilot Marketing Inc.

Email: dan.kubb@autopilotmarketing.com
Phone: 1 (604) 820-0212
Web:   http://autopilotmarketing.com/
vCard: http://autopilotmarketing.com/~dan.kubb/vcard
__________________________________________________________________



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (Darwin)

iD8DBQFF6nWO4DfZD7OEWk0RAhpXAJsH0ZYbp1Rt5aNZEIUtTa7Z8q9d3ACdHwdm
X6whYvFwH02fEBYi1zMF1v0=
=KFsJ
-----END PGP SIGNATURE-----
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-03-04 09:24
(Received via mailing list)
Taylor Strait wrote:
>
> 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.[]
>
> Rails dies when it checks for the non-existent params[:post][:id].  I
> will always send either :event or :post, but never both.  How can I use
> "if - elsif" to test for this without crashing?  Thanks!
>

It depends on what is nil, i.e. params[:post] or params[:post][:id] but
you can do something like:

if params[:post].has_key?(:id)

or

if not params[:post][:id] == nil

--
Michael Wang
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-03-04 11:54
(Received via mailing list)
> Rails gives me an error when I test for a blank param like this:
>
>         if params[:post][:id]
>
> If I have params[:post][:id], things are fine.  But if I have
> params[:event][:id] instead, I get this error:
And a third solution yet
    if defined? params[:post][:id]
    else
    end

It will not always work if you use it from a view (because of how local
variables are handled), but for Models, Controllers and the sort it
should be fine.

regards,
javier ramirez

--------
Estamos de estreno... si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2007-03-04 16:16
Thanks to all - these solutions worked.  I ended up using "if
params[:post] && params[:post][:id]"
This topic is locked and can not be replied to.