未定义的方法“call”对于 nil:NilClass

发布于 2024-12-28 19:26:08 字数 2369 浏览 4 评论 0原文

所以我正在使用 Ryan Bates Simple_Form railscast ,当我遇到以下错误时我尝试提交一个表单:

NoMethodError in Products#index

Showing /home/panos/sites/store/app/views/products/index.html.erb where line #8 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #8):

5:     <h2><%= link_to product.name, product %></h2>
6:     <div>
7:       Price: <%= number_to_currency product.price %><br />
8:       Category: <%= product.category.name %><br />
9:       <%= link_to "Edit", [:edit, product] %>
10:     </div>
11:   <% end %>
Rails.root: /home/panos/sites/store

Application Trace | Framework Trace | Full Trace
app/views/products/index.html.erb:8:in `block in _app_views_products_index_html_erb___762171406_75615480_549568821'
app/views/products/index.html.erb:4:in `each'
app/views/products/index.html.erb:4:in `_app_views_products_index_html_erb___762171406_75615480_549568821'

这是我的 index.html.erb 文件:

<% title "Products" %>

<div class="product">
  <% for product in @products %>
    <h2><%= link_to product.name, product %></h2>
    <div>
      Price: <%= number_to_currency product.price %><br />
      Category: <%= product.category.name %><br />
      <%= link_to "Edit", [:edit, product] %>
    </div>
  <% end %>
</div>

<p><%= link_to "New Product", new_product_path %></p>

这是我的 form.html.erb 文件:

<%= simple_form_for @product do |f| %>
  <%= f.error_messages %>
  <table border="1">
  <tr>
  <td><%= f.input :name %></td>
  <td><%= f.input :price, :hint => "prices should be in USD" %></td>
  <td><%= f.input :released_on %></td>
  <td>  <%= f.association :category, :include_blank => false %></td>
  <td><%= f.input :rating, :collection => 1..5, :as => :radio %></td>
  <td><%= f.input :discontinued %></td>
  <td><%= f.button :submit %></td>
  </tr>

<% end %>

我知道产生错误是因为类别字段为空(nill),但我不知道如何使固定它,以便即使值为空也可以显示。

有人可以帮忙吗?

So I'm playing around with Ryan Bates Simple_Form railscast, and I get the following error when I try to submit a form:

NoMethodError in Products#index

Showing /home/panos/sites/store/app/views/products/index.html.erb where line #8 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #8):

5:     <h2><%= link_to product.name, product %></h2>
6:     <div>
7:       Price: <%= number_to_currency product.price %><br />
8:       Category: <%= product.category.name %><br />
9:       <%= link_to "Edit", [:edit, product] %>
10:     </div>
11:   <% end %>
Rails.root: /home/panos/sites/store

Application Trace | Framework Trace | Full Trace
app/views/products/index.html.erb:8:in `block in _app_views_products_index_html_erb___762171406_75615480_549568821'
app/views/products/index.html.erb:4:in `each'
app/views/products/index.html.erb:4:in `_app_views_products_index_html_erb___762171406_75615480_549568821'

Here is my index.html.erb file:

<% title "Products" %>

<div class="product">
  <% for product in @products %>
    <h2><%= link_to product.name, product %></h2>
    <div>
      Price: <%= number_to_currency product.price %><br />
      Category: <%= product.category.name %><br />
      <%= link_to "Edit", [:edit, product] %>
    </div>
  <% end %>
</div>

<p><%= link_to "New Product", new_product_path %></p>

And here is my form.html.erb file:

<%= simple_form_for @product do |f| %>
  <%= f.error_messages %>
  <table border="1">
  <tr>
  <td><%= f.input :name %></td>
  <td><%= f.input :price, :hint => "prices should be in USD" %></td>
  <td><%= f.input :released_on %></td>
  <td>  <%= f.association :category, :include_blank => false %></td>
  <td><%= f.input :rating, :collection => 1..5, :as => :radio %></td>
  <td><%= f.input :discontinued %></td>
  <td><%= f.button :submit %></td>
  </tr>

<% end %>

I know the error is produced because the Category field was empty (nill), but I dont know how to fix it, so that it can display even with a nill value.

Can anybody help?

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

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

发布评论

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

评论(3

滥情哥ㄟ 2025-01-04 19:26:08

或者更简洁一点。

类别:<%=product.category.name if Product.category %>

Category: <%= Product.category.try(:name) %>

尽管 try 方法被很多人所反对。

Or a little more concisely.

Category: <%= product.category.name if product.category %>
or

Category: <%= product.category.try(:name) %>

Although the try method is frowned on by alot of people.

娜些时光,永不杰束 2025-01-04 19:26:08

试试这个:

Category: <%= product.category.name unless product.category.nil? %>

Try this:

Category: <%= product.category.name unless product.category.nil? %>
﹏半生如梦愿梦如真 2025-01-04 19:26:08

看起来至少对于一行,product.category 的值为零,因此当取消引用product.category 来访问“name”时,会发生异常。

在撰写本文时提供的答案中,“除非”解决方案防止引用 nil。 “try”解决方案在发生异常的情况下安全地捕获异常。

It seems likely that for at least one row, the value of product.category is nil, so an exception is hit when product.category is dereferenced to access 'name'.

Of the answers provided at time of writing, the 'unless' solution guards against referencing nil. The 'try' solution catches the exception safely in the case where the exception occurs.

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