私はこれを一日中デバッグしています。 私のアプリケーションには2つのモデルがあります:teaClass &お茶。ティーラス.rbで、私は持っていますbelongs_toとhas_manyを正しく使うにはどうすればいいですか?
has_many :teas
私は 'belongs_to:teaclass`を持っています。
私はこの"..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" というエラーを取得しておくようなURLが見えるようにしよう
/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
編集に感謝します:D – jchenjc