After adding relationships between tables, an error began to pop out when loading the main page - undefined method `key? ' for nil: NilClass

Here is the default controller:

class StoreController < ApplicationController def index @products = Product.order(:title) end end 

view index

 <% if notice %> <p id="notice"><%= notice %></p> <% end %> <h1>Your Pragmatic Catalog</h1> <% @products.each do |product| %> <div class="entry"> <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <%= sanitize(product.description) %> <div class="price_line"> <span class="price"><%= number_to_currency(product.price, :unit =>" &pound") %></span> <%=button_to 'Add to cart', line_items_path(product_id: product) %> </div> </div> <% end %> 

Model

 class Product < ActiveRecord::Base has_many :line_items before_destroy : ensure_not_referenced_by_any_line_item attr_accessible :title, :description, :image_url, :price validates :title, :description, :image_url, :price, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} validates :title, uniqueness: true def ensure_not_referenced_by_any_line_item if line_items.empty? return true else errors.add(:base, " существуют товарные позиции") return false end end 

detailed trace - http://pastebin.com/iz7TU4XW

  • I don’t promise to answer, but try adding a link to pastebin.com with a detailed trace trace - Stanislav Pankevich
  • rebooted the server, now the error is undefined method each' for 0:Fixnum. Хочу добавить новый продукт, перехожу на .../products/new выдает ошибку undefined method each' for 0:Fixnum. Хочу добавить новый продукт, перехожу на .../products/new выдает ошибку undefined method key? ' for nil: NilClass - dborovsky
  • help please! - dborovsky
  • @dborovsky, please give the model code line_items - Niki-Timofe

1 answer 1

already figured out, before_destroy: ensure_not_referenced_by_any_line_item - extra space