下面是我的index.html.erb(我只是想展現品牌的名單和相關Subbrands)未定義的方法
<h1>Brands</h1>
<% @brands.each do |brand| %>
<h2><%= brand.name %></h2>
<% end %>
<% @brands.subbrands.each do |subbrand| %>
<h2><%= subbrand.name %></h2>
<% end %>
我收到的時候觀看的index.html是錯誤:
undefined method `subbrands' for #<Array:0x9e408b4>
這裏是我的brands_controller:
class BrandsController < ApplicationController
def index
@brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @brands }
end
end
end
這裏是我的routes.rb
Arbitrary::Application.routes.draw do
resources :brands do
resources :subbrands
end
resources :subbrands do
resources :subsubbrands
end
這裏是我的brand.rb模型
class Brand < ActiveRecord::Base
validates :name, :presence => true
has_many :subbrands
has_many :subsubbrands, :through => :subrands
end
...我subbrand.rb模型
class Subbrand < ActiveRecord::Base
validates :name, :presence => true
belongs_to :brand
has_many :subsubbrands
end
當我更改爲: – Abram
謝謝你。當我修改index.html以包含您的建議時,我收到了另一個錯誤:未定義的方法'each'for#
Abram
@Abram:對不起,我省略'brand'和'each'之間的「子品牌」,請參閱我的更新。 –