Entwicklung melden: ActionView::Template::Error (undefined method `name' for nil:NilClass):

Ich bin ein Neuling und diese ist für den Schienen-Lernprogramm von Richard Schneeman. Das ist alles, was in meinem index.html-Code.erb-Datei in meinem view/products Ordner.

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

Das Problem mit <%= product.user.name %>, das gibt den Fehler:

NoMethodError in Produkte#undefined index Methode `name' für
nil:NilClass.

Mein controller-Dateien sind Vanille und meiner Wege.rb hat:

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

Dies ist in den Modellen:

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

Jegliche Hilfe wird sehr geschätzt.

InformationsquelleAutor ltrainpr | 2013-04-22

Schreibe einen Kommentar