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.
@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.
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
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.
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.
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?
Except that it's not mentioned any more - it's commented out
( mentioned is obviously a rather wooly term)
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 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.