1
我有以下設置:Ruby on Rails的未定義的方法'標題」的零:NilClass
Product.rb
class Product < ActiveRecord::Base
belongs_to :category
end
Category.rb
class Category < ActiveRecord::Base
belongs_to :category
has_many :categories
has_many :products
end
categories_controller.rb
def show
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:title, :category_id)
end
products_controller.rb
def product_params
params.require(:product).permit(:title, :price, :text, :category_id, :avatar)
end
類節目
<% @category.products.each do |p| %>
<article class="content-block">
<h3><%= @p.title %></h3>
</article>
<% end %>
這將返回錯誤的稱號。我在這裏做錯了什麼?