Rails 控制器中出现 NoMethodError

发布于 12-09 19:25 字数 1002 浏览 1 评论 0原文

这是在我的控制器

def results
#searches with tags
@pictures = Picture.all
@alltags = Tag.all
searchkey = params['my_input']
pList = []
listsize = 0
while listsize < @pictures.size
  pList[listsize] = 0
  listsize += 1
end
@alltags.each do |tag|
  if searchkey == tag.tagcontent
    pList[tag.picture.id-1] += 1
  end
end
@pictures.each do |picture|
  if searchkey == picture.name
    pList[picture.id-1] += 1
  end
end
@pictures = @pictures.sort {|pic1, pic2| pList[pic2.id-1] <=> pList[pic1.id - 1]}

端,

调用 NoMethodError 时,就会出现此错误

当在 SearchController#results 中

。当您没有预料到时,您有一个 nil 对象! 您可能期望一个 Array 的实例。 评估 nil 时发生错误。+ Rails.root:/Users/kevinmohamed/SnapSort/server

应用程序跟踪 |框架跟踪 |完整追踪 app/controllers/search_controller.rb:31:in 结果块' app/controllers/search_controller.rb:29:in每个' app/controllers/search_controller.rb:29:in `results'

31 是 pList[picture.id-1] += 1 , 29 是 @pictures.each do |picture| ,为什么会发生这个错误

this is in my controller

def results
#searches with tags
@pictures = Picture.all
@alltags = Tag.all
searchkey = params['my_input']
pList = []
listsize = 0
while listsize < @pictures.size
  pList[listsize] = 0
  listsize += 1
end
@alltags.each do |tag|
  if searchkey == tag.tagcontent
    pList[tag.picture.id-1] += 1
  end
end
@pictures.each do |picture|
  if searchkey == picture.name
    pList[picture.id-1] += 1
  end
end
@pictures = @pictures.sort {|pic1, pic2| pList[pic2.id-1] <=> pList[pic1.id - 1]}

end

this error comes when this is called

NoMethodError in SearchController#results

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.root: /Users/kevinmohamed/SnapSort/server

Application Trace | Framework Trace | Full Trace
app/controllers/search_controller.rb:31:in block in results'
app/controllers/search_controller.rb:29:in
each'
app/controllers/search_controller.rb:29:in `results'

31 is pList[picture.id-1] += 1 , 29 is @pictures.each do |picture|, why is this error happening

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆2024-12-16 19:25:31

pList 是一个数组,索引为 0、1、2、3、4...

您的行

pList[picture.id-1] += 1

可能引用了不存在的索引。例如,如果 pList 有 50 个成员,则其索引为 0-49。如果上图的id是7891,那么它会尝试寻找7890的索引,当然这个索引不存在。这将返回 nil,并尝试执行“nil += 1”,这就是错误的来源。

也许 pList 应该是一个由图片 id 键控的哈希?取决于您想要实现的目标。但无论您想要做什么,几乎可以肯定有一种更简洁的方式可以用 Ruby 来表达它。

pList is an array, indexed with 0, 1, 2, 3, 4...

Your line

pList[picture.id-1] += 1

is likely to refer to an index which doesn't exist. For example, if pList has 50 members, it has indecies 0-49. If the id of the above picture is 7891, then it will try to find an index of 7890 which of course doesn't exist. This will return nil, and try to execute "nil += 1", which is where your error is coming from.

Perhaps pList should be a Hash keyed by picture ids? Depends on what you're trying to accomplish. But whatever you're trying to do, there is almost certainly a less verbose way of expressing it in Ruby.

童话里做英雄2024-12-16 19:25:31

当您迭代的某些内容在不期望的情况下产生了 nil 时,就会发生此错误。您的控制器中有很多代码。我建议将其中一些逻辑转移到模型的方法中,并为其编写一些测试,包括在标签或图片不可用时引发错误。然后您可以挽救控制器中的错误以显示更友好的错误消息。

This error is occurring when something you are iterating over produced a nil when it wasn't expecting one. You have a lot of code in your controller. I would suggest moving some of this logic into a method of a model and write some tests for it, including raising of errors when tags or pictures are not available. Then you can rescue the error in the controller to display a more friendly error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文