I have been debugging this the entire day.
I have two models in my application: teaClass & tea. In teaclass.rb, I have
has_many :teas
In tea.rb, I have ‘belongs_to :teaclass`.
I try to make the url looks like this "..teaclasses/:id/teas/:id"; so in teas_controller.rb, I put 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
But I keep getting an error saying “unknown attribute: 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'
Can anyone help me on this? Thank you.
This only matters if you are trying to get nested routes setup but id doesn’t sound like you are doing that.
What you need to do is actually add the foreign id to the database. So check
schema.rband make sure that column exists.Here is what you need to do.
tea.routes.rb
Models
models/tea.rb
tea_class.rb
controllers
teas_controller.rb
views
This is really important. Make sure you are passing the :tea_class_id as a parameter when you create a tea otherwise it doesn’t know how to make the association. It’s kinda behind the stage because you send
params[:tea]but it is in those parameters where thetea_class_idis actually sent.So… in your view you need to have some sort of way for users to choose a category or as you have it tea class and that is usually done with a select box when it is a one to many association.
new.html.erb
Make sure you have tea classes to actually fill the
collection_selectmethod. Google that plus rails api if you don’t get what’s going on.Nested Routes (on the side)
Looked like you were trying to get the routes like
teaclasses/:id/teas/:id. This is called nested routing and you will want to set that up in your routes.rbThen you can link to
teas_classes/pour/teas/chinese. You should know this commandrake routes. It will help you understand how the paths work.But if you just want the link to get going it should be like this:
You need to supply
@teasdo the link because it takes the id from that and when you click it gives it to theteas_controller' asparams[:teas_class_id]`. You don’t need to do anything without. It will automatically be in the url.