user2396842
user2396842

Reputation: 23

"Undefined method `association' " error when adding form check boxes for associated model

I'm a bit of a Rails noob so sorry if this is an obvious mistake on my part. I am trying to create an association two models, Images and Categories, where each image instance has a 'has_and_belongs_to_many' relationship with categories and vice versa. I want to get the categories to show up in the image upload form as check boxes to allow each image to be attached to multiple categories but keep getting this error:

undefined method `association' for #<ActionView::Helpers::FormBuilder:0x000001011cf370>

I have been stuck on it for about 2 days now and checked every other post that looked like a similar problem as well as Googling, trying various tutorials and starting from scratch several times. Nothing has worked so far and I've kind of hit a dead end with it. Here is what I have so at the moment. The categories and images models work fine on their own and as far as I can tell the categories_images migration is fine too. I've checked the tables and can insert data into images and categories from the console and the GUI. All works fine but just can't figure why the form is throwing this error when I try to make the association. Any help on this is really appreciated!

Here is what I have at the moment. I am on Rails 3.2.11 and Ruby 1.9.3 if that's any help!

Image.rb

class Image < ActiveRecord::Base
 attr_accessible :description, :name
 has_and_belongs_to_many :categories
 accepts_nested_attributes_for :categories

end

Category.rb

class Category < ActiveRecord::Base
  attr_accessible :name
  validates_presence_of :name
  has_and_belongs_to_many :images
end

categories_images.rb

class CategoriesImages < ActiveRecord::Migration
  def change
  create_table :categories_images do |t|
   t.integer :category_id
   t.integer :image_id
  end
  add_index :categories_images, [:category_id,:image_id]
 end
end

create_images.rb

class CreateImages < ActiveRecord::Migration

  def change
    create_table :images do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
   end
  end

create_categories.rb

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name

      t.timestamps
    end
  end
end

This is the image upload form (minus any file upload capability at the moment) with the 'f.association' line that seems to be the problem highlighted.

<%= form_for(@image) do |f| %>
  <% if @image.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>

      <ul>
      <% @image.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
**<%= f.association :categories, :as => :checkboxes %>**
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Upvotes: 2

Views: 1782

Answers (1)

toms
toms

Reputation: 2062

The error says that 'association' is not an available method. Is there a dependency on a gem like simple_form? Are you including this?

Upvotes: 1

Related Questions