我一直在調試這一整天。 我在我的應用程序中有兩個模型:teaClass &茶。在teaclass.rb中,我有如何正確使用belongs_to和has_many?
has_many :teas
在tea.rb中,我有'belongs_to:teaclass`。
我儘量使URL看起來像這樣"..teaclasses/:id/teas/:id";
所以在teas_controller.rb
,我把before_filter :get_teaClass
def show
@tea = @teaclass.teas.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @tea }
end
end
def new
if @teaclass.teas
@tea = @teaclass.teas.new
@teaclass.teas << @tea
#@tea = Tea.new
else
flash[:notice=>"failed"]
@tea = Tea.new
@teaclass.teas << @tea
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @tea }
end
end
def get_teaClass
begin
@teaclass = Teaclass.find(params[:teaclass_id])rescue
redirect_to teaclass_path, :notice => "Teaclass Required!"
end
end
但我不斷收到一個錯誤說 「未知屬性:teaclass_id」
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:inassign_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2178:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:376:in `method_missing'
/home/jianpchen/repo/Teashop/app/controllers/teas_controller.rb:31:in `new'
任何人都可以幫我解決這個問題?謝謝。
確保你所有的代碼都是正確的,我編輯了它的出來 – s84 2011-01-08 03:40:09
謝謝編輯:D – jchenjc 2011-01-08 03:53:01