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

发布于 2024-10-26 02:55:46 字数 622 浏览 4 评论 0原文

你好 我有一个模型类别和另一个模型产品。它们具有关系 - has_and_belongs_to_many。

当我打开控制台并输入 Category.first.products 时,我会收到一个列表,其中包含附加到该类别的产品。

但是,当我尝试使用 show 模型生成 xml 文件时,我得到:

undefined method `type' for nil:NilClass

我已经在其他模型上测试了下面的代码,我已经建立了 has_many 关系,并且它的工作方式就像我希望的那样工作。但不会与这种关系合作,也许与此无关?

  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

......

正如你所看到的,我对此很陌生

Hi
I got a model Category and another model Products. The have the relationship - 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.

But when I try to generate an xml file with the show model I get:

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. 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...

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

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

发布评论

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

评论(2

任谁 2024-11-02 02:55:46

感谢您的回复,我继续调查并尝试了控制器,然后尝试了一下。 @category.products。全部,它起作用了。可悲的是,我不知道为什么会这样......特别是当它在控制台中对我有用时......但我想我很高兴以一种方式解决了这个问题......

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

结束

但当我比以前调试更多时,我会使用日志和终端记录器!感谢您的提示!

Thanks for your replies, I continued investigating and experimented with the controllers, and just tried with. @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!

百善笑为先 2024-11-02 02:55:46

在这种情况下,您的问题似乎是由一些奇怪(可能不一致)的数据引起的。尝试查看(在您的控制台中)导致问题的对象 - 在您的控制器中您使用了一些特定的 ID。检查 Category.find(THIS_IS).products.to_xml 是否也引发错误。

检查单个产品是否能够返回有用的内容来响应 .to_xml。

你会经常遇到类似的问题。您应该学习如何调试它们。

首先是观察回溯。您可能会在 Web 浏览器中看到它,但所有异常通常也会放置在您的 log/development.log 中(取决于环境)。

在开发日志中,您可以看到错误之前运行的 SQL 查询。有时他们可能会帮助您找出问题所在。在这种情况下,您可以检查在错误发生之前选择了哪种类型的模型。 (这只是一个线索,而不是解决方案)

在回溯中,您会监视已编辑的文件。通常错误会出现在您的代码中。如果您的代码看起来不错,您可能想查看回溯中提到的其他文件。阅读 Rails 的代码可能会给你一些线索。有时。

还有一个问题:您是否弄乱了模型中名为“type”的属性?如果是,则尝试将此类属性重命名为其他名称。

我们无法给您更准确的答案。我希望你能找到问题所在。另外,在开始修复错误之前,请尝试为此编写一个单元测试:检查在测试数据库中的模型之一上调用 .to_xml 是否不会引发异常。

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. Check whether Category.find(THIS_IS).products.to_xml trows the error too.

Check whether a single Product is able to return something useful in response to .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).

In the development log you see the SQL queries run before the error. 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. Sometimes.

And a question: have you messed with a property called 'type' in your models? 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.

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