Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在if结构中更改参数会导致rubyonrails中出现错误(nil:NilClass的未定义方法';[]';)_Ruby On Rails_Ruby On Rails 3_If Statement - Fatal编程技术网

Ruby on rails 在if结构中更改参数会导致rubyonrails中出现错误(nil:NilClass的未定义方法';[]';)

Ruby on rails 在if结构中更改参数会导致rubyonrails中出现错误(nil:NilClass的未定义方法';[]';),ruby-on-rails,ruby-on-rails-3,if-statement,Ruby On Rails,Ruby On Rails 3,If Statement,我在index.html.erb视图中有一个搜索和筛选表。根据视图中单击的内容,我必须更改参数。但奇怪的是,即使不满足if语句条件,我也会得到一个错误 例如,在我的代码中,我有: if params[:checkedA] != nil params = someVar end if params[:checkedB] != nil #run code else #run other code end 当我只分配一个随机变量而不是params或者去掉第一个if语句时,它就工作了 if

我在index.html.erb视图中有一个搜索和筛选表。根据视图中单击的内容,我必须更改参数。但奇怪的是,即使不满足if语句条件,我也会得到一个错误

例如,在我的代码中,我有:

if params[:checkedA] != nil
  params = someVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end
当我只分配一个随机变量而不是params或者去掉第一个if语句时,它就工作了

if params[:checkedA] != nil
  foo = someOtherVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end
这同样有效

if params[:checkedB] != nil
  #run code
else
  #run other code
end
但是,即使if语句不应该运行,它仍然会失败。为了测试它,我试着

if 2 < 1
  params = someVar
end
if params[:checkedB] != nil
  #run code
else
  #run other code
end
如果2<1
params=someVar
终止
如果参数[:checkedB]!=无
#运行代码
其他的
#运行其他代码
终止
错误:

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
ActionView::Template::Error(nil:NilClass的未定义方法“[]”):

1:根据霍尔格刚才的评论:


Ruby使用nil初始化名为params的新局部变量(在本例中,它会隐藏同名的方法),即使代码没有实际执行。Ruby对所有可能的赋值都这样做,不管它们是否实际执行,就像在Ruby中一样,变量的创建是赋值的一个单独步骤。

显然params是nil。确保params不是nil,或者检查params&¶ms[…]Ruby的可能副本是否使用
nil
初始化名为
params
的新局部变量(在您的情况下,它会隐藏相同名称的方法),即使代码没有实际执行。Ruby对所有可能的赋值都这样做,不管它们是否实际执行,就像在Ruby中一样,变量的创建是赋值的一个单独步骤。@HolgerJust好吧,我会被诅咒的。我想你知道的越多。如果你把它写在一个答案里,我可以接受它,否则我就发布我的解决方案。@notblakeshelton类似的问题已经被问了很多次,因此上面的链接重复了。不需要额外的答案来解释同样的事情。