简体   繁体   English

开发日志:ActionView :: Template :: Error(未定义的方法`name'代表nil:NilClass):

[英]Development log: ActionView::Template::Error (undefined method `name' for nil:NilClass):

I'm a newbie and this is for the Rails tutorial by Richard Schneeman. 我是新手,这是Richard Schneeman撰写的Rails教程。 This is all that is in my index.html.erb file in my view/products folder. 这就是我的view / products文件夹中的index.html.erb文件中的所有内容。

<% first_product = Product.first %>
<% lots_of_products = Product.includes(:user).all %>
<ul>
  <% lots_of_products.each do |product| %>
    <li>
      Product Name: "<%= product.name %>"" costs $<%= product.price %>
      Sold by <%= product.user.name %>
    </li>
  <% end %>
</ul>

The issue is with <%= product.user.name %> , which gives the error: 问题出在<%= product.user.name %> ,它给出了错误:

NoMethodError in Products#index undefined method `name' for nil:NilClass. 产品中的NoMethodError #index未定义方法`name'代表nil:NilClass。

My controller files are vanilla and my routes.rb has: 我的控制器文件是vanilla,我的routes.rb有:

ControllerExercise::Application.routes.draw do
  get '/products' => 'products#index'
  resources :users
end

This is in the models: 这是在模型中:

class Product < ActiveRecord::Base
  belongs_to :user
  attr_accessible :name, :price
end

class User < ActiveRecord::Base
  has_many :products
  attr_accessible :job_title, :name
end

Any and all help is greatly appreciated. 非常感谢任何和所有的帮助。

你也可以试试

product.user.try(:name)

In your database i suspect some of your product data does not contain sold_by_id or user_id. 在您的数据库中,我怀疑您的某些产品数据不包含sold_by_id或user_id。 So its getting product.user nil as no user is associated with the product. 所以它获得product.user nil,因为没有用户与产品相关联。 Instead of 代替

<%= product.user.name %>

use 采用

<%= product.user.name if product.user %>

to skip this exception. 跳过此例外。

So your index.html.erb becomes 所以你的index.html.erb变成了

<% lots_of_products = Product.includes(:user).all %>
<ul>
  <% lots_of_products.each do |product| %>
<li>
  Product Name: <%= product.name %> costs $<%= product.price %>
  Sold by <%= product.user.name if product.user %>
</li>
<% end %>
</ul>

I also came up against this and used a simple if else statement, because I still wanted to show something even if the username didn't exist 我也反对这个,并使用了一个简单的if else语句,因为即使用户名不存在,我仍然希望显示一些东西

<!-- SHOW USERNAME IF IT EXISTS -->

<% if product.user %>
   <p><strong><%= product.user.name %></strong></p>
<% else %>
   <p><strong>Anonymous</strong></p>
<% end %>

A great workaround to this is ruby's safe access operator &. 一个很好的解决方法是ruby的安全访问操作员&。

see http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/ http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/

It was invented by Matz (ruby's founder) for this very situation. 它是由Matz(ruby的创始人)为这种情况发明的。 In your case it would look like 在你的情况下,它看起来像

product.user&.name

It returns nil if the name attribute is not defined. 如果未定义name属性,则返回nil。 Keep in mind its only available in ruby 2.3.0 and higher. 请记住它仅适用于ruby 2.3.0及更高版本。

暂无
暂无

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

相关问题 错误ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Error ActionView::Template::Error (undefined method `name' for nil:NilClass) Heroku ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Heroku ActionView::Template::Error (undefined method `name' for nil:NilClass) Heroku 问题,ActionView::Template::Error(nil:NilClass 的未定义方法“名称”): - Heroku issue, ActionView::Template::Error (undefined method `name' for nil:NilClass): 使用ActionView :: Template :: Error测试失败:nil的未定义方法`name&#39;:NilClass - Test fail with ActionView::Template::Error: undefined method `name' for nil:NilClass ActionView :: Template :: Error:未定义的方法&#39;&lt;&#39;为nil:NilClass - ActionView::Template::Error: undefined method `<' for nil:NilClass ActionView :: Template :: Error:ActionView :: Template :: Error:未定义的方法`[]&#39;为nil:NilClass - ActionView::Template::Error: ActionView::Template::Error: undefined method `[]' for nil:NilClass ActionView :: Template :: Error(nil:NilClass的未定义方法“ strip!”) - ActionView::Template::Error (undefined method `strip!' for nil:NilClass) FactoryGirl Rspec ActionView :: Template :: Error:nil:NilClass的未定义方法 - FactoryGirl Rspec ActionView::Template::Error: undefined method for nil:NilClass ActionView :: Template :: Error(nil:NilClass的未定义方法“ event_title”) - ActionView::Template::Error (undefined method `event_title' for nil:NilClass) ActionView :: Template :: Error(nil:NilClass的未定义方法“ force_encoding”): - ActionView::Template::Error (undefined method `force_encoding' for nil:NilClass):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM