Forum: Ruby on Rails rails 2.3 nested forms with has_many through checkboxes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E764ca39b6f05d2cde73f0cd1c49ef18?d=identicon&s=25 OKR (Guest)
on 2009-04-12 23:48
(Received via mailing list)
I was wondering if anyone knew of a way to combine the new nested
forms in rails 2.3 with a has_many through relationship via
checkboxes.  Basically I have a page model, a page_category model and
a category_items join table.  Pages have many page categories through
category items.  The category_items table is polymorphic so i can use
it for other models who need categories (maybe this is
overcomplicating everything though?).

In a page's edit form I want to be able to dynamically add new
category fields and then associate the new ones I create and the other
already created page categories to the current page via checkboxes.
Hopefully this makes sense and can actually be done, I'd rather not
have to go to another controller/form just to create and edit page
categories.

class Page < ActiveRecord::Base
        has_many :category_items, :as => :item, :dependent => :destroy
  has_many :page_categories, :through => :category_items, :source
=> :category, :source_type => "PageCategory", :uniq => true
  accepts_nested_attributes_for :page_categories, :allow_destroy =>
true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class PageCategory < ActiveRecord::Base
  has_many :pages, :through => :category_items, :source
=> :item, :source_type => 'Page', :uniq => true
end

class CategoryItem < ActiveRecord::Base
  belongs_to :category, :polymorphic => true
  belongs_to :item, :polymorphic => true
end

Page Category partial for my page form (hooks into a jquery script to
dynamically add/remove page category form fields)...

<a href="#" id="page_categories" class="header">Categories<%=
hidden_field_tag "page[page_category_ids][]", "" %></a>
<ul class="submenu">
  <%- for category in PageCategory.all -%>
  <li>
    <%= check_box_tag "page[page_category_ids][]", category.id,
@page.page_categories.include?(category), :class => "checkbox"
%>&nbsp;<% f.fields_for category do |category_form| %><%=
category_form.text_field :title, :style => "width: 200px;", :class =>
"textfield small formElement" %><% end %>
  </li>
  <%- end -%>
</ul>

Right now, if I try and create or update a page I get the following
error...

ActiveRecord::UnknownAttributeError in Admin/pagesController#update

unknown attribute: page_category
RAILS_ROOT: /Users/rajo/Sites/boilerplate

Application Trace | Framework Trace | Full Trace
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/
active_record/base.rb:2745:in `attributes='
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/
active_record/base.rb:2741:in `each'
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/
active_record/base.rb:2741:in `attributes='
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/
active_record/base.rb:2627:in `update_attributes'
/Users/rajo/Sites/boilerplate/app/controllers/admin/
pages_controller.rb:107:in `update'
/Users/rajo/Sites/boilerplate/app/controllers/admin/
pages_controller.rb:106:in `update'
Request

Parameters:

{"commit"=>"Save",
 "_method"=>"put",
 "authenticity_token"=>"DvvJPsU9c942x1Ak5wOIm8V4wYv8AClftKJ7ohGqOo0=",
 "id"=>"about",
 "page"=>{"name"=>"About",
 "title"=>"About Us",
 "body"=>"<p>About this site.</p>",
 "published"=>"1",
 "page_category"=>{"title"=>"Default Two"},
 "page_category_ids"=>["",
 "1",
 "2"],
 "home"=>"0"}}
Show session dump

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}

Thanks for any help in advance.
This topic is locked and can not be replied to.