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
Category
<% for category in @supplier_categories %>
<%= check_box_tag('supplier_profile[supplier_category_ids][]', category.id) %>
<%= category.name %>
<% 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