eireanngobrach87-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2013-Jul-28 16:36 UTC
HABTM Checkboxes
I have a Course model which habtm categories. I am trying to have checkboxes to add or delete categories from the new and update courses pages. Models: course.rb # == Schema Information # # Table name: courses # # id :integer not null, primary key # course_number :integer # partner :string(255) # title :string(255) # length :integer # includes_certification :boolean # overview :text # price :integer # created_at :datetime # updated_at :datetime # more_link :string(255) # class Course < ActiveRecord::Base self.per_page = 8 has_and_belongs_to_many :course_dates has_and_belongs_to_many :categories attr_accessible :course_number, :partner, :title, :length, :includes_certification, :overview, :more_link, :price, :tags, :course_dates_attributes, :categories_attributes accepts_nested_attributes_for :course_dates, allow_destroy: true accepts_nested_attributes_for :categories, allow_destroy: true end category.rb # == Schema Information # # Table name: categories # # id :integer not null, primary key # name :string(255) # created_at :datetime # updated_at :datetime # class Category < ActiveRecord::Base has_and_belongs_to_many :courses attr_accessible :name end My form is being built from this partial: = simple_form_for(@course) do |f| = f.error_notification .form-inputs = f.input :course_number = f.input :partner = f.input :title = f.input :length = f.fields_for :course_dates do |builder| = render ''course_date_fields'', f: builder = link_to_add_fields "Add Date", f, :course_dates = f.input :includes_certification = f.input :overview = f.input :more_link = hidden_field_tag "course[category_ids][]", nil - Category.all.each do |category| = check_box_tag "course[category_ids][]", category.id, @course.category_ids.include?(category.id), id: dom_id(category) = label_tag dom_id(category), category.name = f.input :price .form-actions = f.button :submit I tried using simple_form''s f.association as: :check_boxes and also the f.collection_check_boxes but both end in the same problem. I can see the checkboxes but when I click update it doesn''t save the categories I checked or unchecked. I have been trying to figure out why, figured it was a problem with mass assignment but I don''t know what else I can do with attr_accessible to make it work. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/28af85fc-5a3a-4b48-9aa6-bcab1a6dd18c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Have a search on Railscasts. This is covered there. Walter On Jul 28, 2013, at 12:36 PM, eireanngobrach87-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I have a Course model which habtm categories. I am trying to have checkboxes to add or delete categories from the new and update courses pages. > > Models: > > course.rb > # == Schema Information > # > # Table name: courses > # > # id :integer not null, primary key > # course_number :integer > # partner :string(255) > # title :string(255) > # length :integer > # includes_certification :boolean > # overview :text > # price :integer > # created_at :datetime > # updated_at :datetime > # more_link :string(255) > # > > class Course < ActiveRecord::Base > self.per_page = 8 > has_and_belongs_to_many :course_dates > has_and_belongs_to_many :categories > attr_accessible :course_number, :partner, :title, :length, :includes_certification, :overview, :more_link, :price, :tags, :course_dates_attributes, :categories_attributes > accepts_nested_attributes_for :course_dates, allow_destroy: true > accepts_nested_attributes_for :categories, allow_destroy: true > end > > > > category.rb > # == Schema Information > # > # Table name: categories > # > # id :integer not null, primary key > # name :string(255) > # created_at :datetime > # updated_at :datetime > # > > class Category < ActiveRecord::Base > has_and_belongs_to_many :courses > attr_accessible :name > end > > > My form is being built from this partial: > > = simple_form_for(@course) do |f| > = f.error_notification > > .form-inputs > = f.input :course_number > = f.input :partner > = f.input :title > = f.input :length > = f.fields_for :course_dates do |builder| > = render ''course_date_fields'', f: builder > = link_to_add_fields "Add Date", f, :course_dates > = f.input :includes_certification > = f.input :overview > = f.input :more_link > = hidden_field_tag "course[category_ids][]", nil > - Category.all.each do |category| > = check_box_tag "course[category_ids][]", category.id, @course.category_ids.include?(category.id), id: dom_id(category) > = label_tag dom_id(category), category.name > = f.input :price > > .form-actions > = f.button :submit > > I tried using simple_form''s f.association as: :check_boxes and also the f.collection_check_boxes but both end in the same problem. I can see the checkboxes but when I click update it doesn''t save the categories I checked or unchecked. > > I have been trying to figure out why, figured it was a problem with mass assignment but I don''t know what else I can do with attr_accessible to make it work. > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/28af85fc-5a3a-4b48-9aa6-bcab1a6dd18c%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2F6A9DFB-3714-4962-B572-8574A76B500D%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.