Hello all,
I''m trying to get my head round HABTM, and need to build a checkbox
list. I have the following models
class Supplier < User
has_one :supplier_profile, :dependent => :destroy
has_one :profile, :foreign_key => ''user_id'', :dependent
=> :destroy
end
class SupplierProfile < ActiveRecord::Base
belongs_to :supplier
has_and_belongs_to_many :supplier_categories
end
class SupplierCategory < ActiveRecord::Base
has_and_belongs_to_many :supplier_profiles
end
and I have my link table: supplier_categories_supplier_profiles
I have a controller, suppliers_controller, a snippet of which looks like
def new
@supplier = Supplier.new
@supplier_profile = @supplier.supplier_profile =
SupplierProfile.new
@supplier_categories = SupplierCategory.find(:all)
end
def create
@supplier = Supplier.new(params[:supplier])
@supplier_profile = @supplier.supplier_profile =
SupplierProfile.new(params[:supplier_profile])
if @supplier.save
# flash[:notice] = ''Link was successfully created.''
# redirect_to :action => ''list''
render :text => "saved"
else
# render :action => ''new''
render :text => "not saved"
end
end
and the checkbox area of my form looks like
<p><label for="url">Category</label><br/>
<% for category in @supplier_categories %>
<p><%=
check_box_tag(''supplier_profile[supplier_category_ids][]'',
category.id) %>
<label for="supplier_profile"><%= category.name
%></label></p>
<% end %>
Basically it''s not saving and I''m not entirely sure why not.
If I
remove the supplier from the new and create methods then it will save
the supplier_profile with any checked checkboxes but something isn''t
quite right with the supplier.
Any ideas?
Alastair
------
Alastair Moore
Standards compliant web development with Ruby On Rails, PHP and ASP
www.kozmo.co.uk
07738 399038
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---