Params being made nil inexplicably

Summary of problem: my params hash is being made nil in my controller
without any reason that I can identify.

Everything was fine until I put this new code as the first lines of code
in my controller. But the block is not being executed because the puts
statement is not evaluated:

def calculate

  if params[:id]
    puts "in if statement"
    @query = Query.where(:id => params[:id])
    params = eval(@query[0].queryString).to_options
    params[:query_end_date].to_options!
    params[:query_start_date].to_options!
  end

The controller then fails in the first line of the next block of code
(below), and the error is that I am evaluating a nil object.

  if params[:entity_selection]=="ELB"
    @instances_array = get_instances_array(params[:instance])
    @grouped_elb_instances =

get_grouped_elb_instances(@instances_array, params[:instance])
else
@instances_array = []
@grouped_elb_instances = []
end

If I comment out the first ‘if’ block, the controller works perfectly.
When I print the params before the if statement, it has a value, but for
some reason, it seems like the params variable is cleared from memory.
I have no idea what is happening.

Any ideas?

On 15 September 2011 22:42, Jonathan S. [email protected] wrote:

Any ideas?

Are you sure the “if” is not getting run through? Change the “puts”
to a “raise”…

On Thu, Sep 15, 2011 at 17:42, Jonathan S. [email protected] wrote:

But the block is not being executed because the puts
statement is not evaluated:

def calculate

if params[:id]
puts “in if statement”

Where are you looking for the output of the puts? On the web page?
In the log? On the screen of the server? In too many faces? :wink:

-Dave


LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web
site.
Main Web S.: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com

@Michael: I’ll give this a shot soon and let you know what happens.

@Dave: I was looking for the output on the screen of the server. Should
I look somewhere else?

I’d have to post the entire controller to show you this, but when I
included my begin-rescue-ensure block (with the criminal if statement at
the top of the begin block), the error still happens, but not until the
ensure block…even though the params hash is referenced multiple times
before then.

I can post the whole controller if that would be helpful, but in any
case, I’ll see what happens with a raise statement in the criminal if.

Let me know if you have any other suggestions.

JS

Michael P. wrote in post #1022225:

On 15 September 2011 22:42, Jonathan S. [email protected] wrote:

Any ideas?

Are you sure the “if” is not getting run through? Change the “puts”
to a “raise”…

@Fred,

Thanks man. I agree, that could be the problem. However, to test it out,
I commented out everything inside the if part of the ‘criminal if-else’
and no exception is raised. For example, the new code that works is:

def calculate

  #Check if request is from a linked URL or direct request from site
  puts "before if statement"
  puts params[:entity_selection]

  if params[:id]
    #puts "in if statement"
    #@query = Query.where(:id => params[:id])
    #params = eval(@query[0].queryString).to_options
    #params[:query_end_date].to_options!
    #params[:query_start_date].to_options!
  else
    puts "in else"
    puts params[:entity_selection]
  end

So if it were the case that the params hash being mentioned could
overwrite it, then this version of the code would fail, right?

I appreciate the help. I’ll continue to try to figure this out. If this
doesn’t work, it could be anarchy out there. I’m worried.

On Sep 16, 2:58pm, “Jonathan S.” [email protected] wrote:

puts params[:entity_selection]
end

So if it were the case that the params hash being mentioned could
overwrite it, then this version of the code would fail, right?

Except that it’s not mentioned any more - it’s commented out
( mentioned is obviously a rather wooly term)

Fred

On Sep 15, 10:42pm, “Jonathan S.” [email protected] wrote:

puts "in if statement"
@query = Query.where(:id => params[:id])
params = eval(@query[0].queryString).to_options
params[:query_end_date].to_options!
params[:query_start_date].to_options!

end

Local variables are tricky - merely mentioning them causes them to
spring into existence. For example if you run

if false
x = 2
end

Then the local variable x is created, with value nil.

I think the same thing is happening here - a local variable called
params is being created, which is shadowing the params instance
method.

Fred

return 3 is the right! the value dont change and never in into if,
because
you put false…its right

2011/9/16 Jonathan S. [email protected]

I see what you mean. However,

x=3
if false
x=2
end

puts x

This code outputs 3, not nil. And I think that’s a more appropriate
analogy. I really don’t understand what’s happening though.

Frederick C. wrote in post #1022322:

On Sep 16, 2:58pm, “Jonathan S.” [email protected] wrote:

puts params[:entity_selection]
end

So if it were the case that the params hash being mentioned could
overwrite it, then this version of the code would fail, right?

Except that it’s not mentioned any more - it’s commented out
( mentioned is obviously a rather wooly term)

Fred

On 16 September 2011 16:07, Jonathan S. [email protected] wrote:

@leoncio:

I was responding to Fred’s suggestion that params was being overwritten
because it was mentioned in the if statement in the original code.
Forget my replies to Fred, do you have any idea why the params variable
is being made nil in the original post?

Use ruby-debug to break into the code and see what is happening. See
the Rails Guide on debugging if you do not know how to do that.

Colin


gplus.to/clanlaw

@leoncio:

I was responding to Fred’s suggestion that params was being overwritten
because it was mentioned in the if statement in the original code.
Forget my replies to Fred, do you have any idea why the params variable
is being made nil in the original post?

leoncio caminha wrote in post #1022336:

return 3 is the right! the value dont change and never in into if,
because
you put false…its right

2011/9/16 Jonathan S. [email protected]

On Sep 16, 3:48pm, “Jonathan S.” [email protected] wrote:

I see what you mean. However,

x=3
if false
x=2
end

An even better example is

puts xyz # should raise name error

if false
xyz = 1
end

puts xyz #=> nil

The thing is that params isn’t initially a local variable - it’s a
method that then gets shadowed by a local variable, i.e.

def xyz
“moo”
end

puts xyz.inspect #=> ‘moo’
if false
xyz = 1
end
puts xyz.inspect #=> nil

Fred

On Sep 15, 5:42pm, “Jonathan S.” [email protected] wrote:

puts "in if statement"
@query = Query.where(:id => params[:id])
params = eval(@query[0].queryString).to_options

Unrelated, but in the name of all that is holy don’t do this. Look
into the ‘serialize’ method:

for a cleaner, faster, safer way to stash whole data structures in the
DB.

–Matt J.

PS: Also, Query.find(params[:id]) is a good bit more understandable
than doing a where and then getting the first element.

@Matt,

Serializing is definitely the right way to go. I’ll get into that. And
of course, I’ll use the find method on the query.

Awesome advice. Very appreciative.

Matt J. wrote in post #1022343:

On Sep 15, 5:42pm, “Jonathan S.” [email protected] wrote:

puts "in if statement"
@query = Query.where(:id => params[:id])
params = eval(@query[0].queryString).to_options

Unrelated, but in the name of all that is holy don’t do this. Look
into the ‘serialize’ method:

ActiveRecord::Base

for a cleaner, faster, safer way to stash whole data structures in the
DB.

–Matt J.

PS: Also, Query.find(params[:id]) is a good bit more understandable
than doing a where and then getting the first element.

@Fred, Matt:

Awesome example and good explanation. I’ll have to dig into this issue
as well.

Thanks to everyone for the help! Amazing group.

On 16 September 2011 16:07, Jonathan S. [email protected] wrote:

@leoncio:

I was responding to Fred’s suggestion that params was being overwritten
because it was mentioned in the if statement in the original code.
Forget my replies to Fred, do you have any idea why the params variable
is being made nil in the original post?

I think perhaps you are thinking that a local variable created inside
an if block will disappear at the end of the block, so that the
original variable is restored. Wrong.

Colin

Posted via http://www.ruby-forum.com/.


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw