1

I have a search and filter table on my index.html.erb view. Depending on what is clicked in the view, I have to change the params. What is weird though, is that I get an error even if the if statement condition isnt met.

So for example, in my code, i have:

if params[:checkedA] != nil
  params = someVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end

When I just assign a random variable instead of params or get rid of the first if statement, it works.

if params[:checkedA] != nil
  foo = someOtherVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end

This also works

if params[:checkedB] != nil
  #run code
else
  #run other code
end

However, even if the if statement shouldn't run, it still fails. To test it, I tried

if 2 < 1
  params = someVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end

The error:

ActionView::Template::Error (undefined method `[]' for nil:NilClass):
  1: <% if 2 < 1
  2:      params = someVar
  3:    end
  4:    if params[:checkedB] != nil
  5:      #run code
  6:    else
  7:      #run other code
  app/views/quotes/quoteTable_js.js.erb:4:in `_app_views_products_product_table_js_js_erb

How can the if statement cause an error if it doesn't even run? Shouldn't that be the same as the statement not even being there?

notblakeshelton
  • 364
  • 1
  • 7
  • 20
  • Apparently params is nil. Ensure that params are not nil or check with if params && params[..] – Kostas Rousis Jun 04 '14 at 16:18
  • possible duplicate of [Why does Ruby seem to hoist variable declarations from inside a case statement even if that code path is not executed?](http://stackoverflow.com/questions/12928050/why-does-ruby-seem-to-hoist-variable-declarations-from-inside-a-case-statement-e) – Holger Just Jun 04 '14 at 16:21
  • Ruby initializes a new local variable named `params` with `nil` (which in your case shadows the method of the same name) even if the code is not actually executed. Ruby does this for all potential assignments, irregardless if they are actually executed or not, as in Ruby, the creation of variable is a separate step from the assignment. – Holger Just Jun 04 '14 at 16:23
  • @HolgerJust Well I'll be darned. The more you know, I guess. If you put that in an answer, I can accept it or i'll just post my solution. – notblakeshelton Jun 04 '14 at 16:44
  • @notblakeshelton A similar question was already asked many times, hence the linked duplicate above. There's no need for an additional answer explaining the same thing. – Holger Just Jun 04 '14 at 17:09

1 Answers1

0

from Holger Just's comment above:

Ruby initializes a new local variable named params with nil (which in your case shadows the method of the same name) even if the code is not actually executed. Ruby does this for all potential assignments, irregardless if they are actually executed or not, as in Ruby, the creation of variable is a separate step from the assignment.

notblakeshelton
  • 364
  • 1
  • 7
  • 20