简体   繁体   English

Ruby On Rails 3-出现nil异常,并且不明白为什么

[英]Ruby On Rails 3 - Getting nil exception, and doesn't understand why

Hi I got a model Category and another model Products. 嗨,我有一个型号类别和另一个型号产品。 The have the relationship - has_and_belongs_to_many. 具有关系-has_and_belongs_to_many。

When I open the console and type Category.first.products, I receive a list with the products attached to that Category. 当我打开控制台并键入Category.first.products时,会收到一个列表,其中包含该类别的产品。

But when I try to generate an xml file with the show model I get: 但是,当我尝试使用show模型生成xml文件时,我得到了:

undefined method `type' for nil:NilClass

I've tested the code below on other models I've made that has_many relationships and it works like how I want it to work. 我已经在具有has_many关系的其他模型上测试了以下代码,其工作方式与我希望它如何工作一样。 But won't work with this relationship, maybe it doesn't have anything to with that? 但是,这种关系将无法解决,也许与此无关吗?

  def show

@categories = Category.find(params[:id])

@products = @categories.products

respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @products }
  format.json { render :json => @products }
end

end 结束

I'm new to this as you can see... 如您所见,我是新手。

Thanks for your replies, I continued investigating and experimented with the controllers, and just tried with. 感谢您的答复,我继续研究控制器并进行了实验,并尝试了。 @category.products. @ category.products。 all , and it worked. 全部 ,并且有效。 The sad thing is that I don't know why it did... Especially when it works for me in the console... But I guess I'm happy to have solved the problem in one way so to speak... 可悲的是,我不知道为什么会这么做...尤其是当它在控制台中对我有用时...但是我想我很高兴以一种方式解决了这个问题……

def show
@category = Category.find(params[:id])

@products = @category.products.all

respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @products }
end

end 结束

But I will use the log and the terminal logger when I debug more than before! 但是当我调试比以前更多时,我将使用日志和终端记录器! Thanks for the tips! 感谢您的提示!

In this case it looks like your problem is caused by some strange (maybe inconsistent) data. 在这种情况下,您的问题似乎是由一些奇怪的(可能是不一致的)数据引起的。 Try to see (in your console) the object which caused the problem - in your controller you have used some specific ID. 尝试查看(在控制台中)导致问题的对象-在控制器中,您使用了特定的ID。 Check whether Category.find(THIS_IS).products.to_xml trows the error too. 检查Category.find(THIS_IS).products.to_xml .products.to_xml是否也抛出该错误。

Check whether a single Product is able to return something useful in response to .to_xml. 检查单个产品是否能够返回有用的内容以响应.to_xml。

You will often meet similar problems. 您将经常遇到类似的问题。 You should learn how to debug them. 您应该学习如何调试它们。

First thing is to watch the backtrace. 第一件事是观察回溯。 You probably see it in your web browser, but all exceptions usually are also placed in your log/development.log (depends on the environment). 您可能会在Web浏览器中看到它,但是所有异常通常也都放置在您的log / development.log中(取决于环境)。

In the development log you see the SQL queries run before the error. 在开发日志中,您会看到SQL查询在错误发生之前运行。 Sometimes they may help you to find what's wrong. 有时,它们可能会帮助您找到问题所在。 In this case you may check what type of model was SELECT-ed just before the error. 在这种情况下,您可以检查在错误发生之前选择了哪种类型的模型。 (this will be just a clue, not a solution) (这只是一个提示,而不是解决方案)

In the backtrace, you watch for the files which you have edited. 在回溯中,您将监视已编辑的文件。 Usually the bug will be in your code. 该错误通常在您的代码中。 If your code looks OK, you may want to see the other files mentioned in the backtrace. 如果您的代码看起来还可以,则可能要查看回溯中提到的其他文件。 Reading Rails' code may give you some clues. 阅读Rails的代码可能会给您一些线索。 Sometimes. 有时。

And a question: have you messed with a property called 'type' in your models? 还有一个问题:您是否在模型中弄乱了一个名为“ type”的属性? If yes, then try to rename such attribute to something else. 如果是,则尝试将此类属性重命名为其他名称。

We cannot give you more precise answer. 我们无法给您更准确的答案。 I hope you will find the problem. 希望您能找到问题所在。 Also, before you start fixing the error, try to write an unit test for this: check whether calling .to_xml on one of your models in test database does not raise an exception. 另外,在开始纠正错误之前,请尝试为此编写一个单元测试:检查在测试数据库中的一个模型上调用.to_xml是否不会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM