gregbell/active_admin — Issue #722: Formtastic form throws “undefined method” error with has_one association
Author: bdastous
Created: 2011-11-04 20:59:49+00:00
State: open

I am using Rails 3.1 and Ruby 1.9.2 along with Active Admin to build a CMS. Here are my Place and Image models:

class Place < ActiveRecord::Base
has_one :image
accepts_nested_attributes_for :image
end

class Image < ActiveRecord::Base
belongs_to :place
end

Here is the Formtastic form I am rendering in my "new" action in my Places controller:

<%= semantic_form_for [:admin, @place] do |p| %>
<%= p.inputs "Details" do %>
<%= p.input :name %>
<%= p.input :description %>
<%= p.input :phone %>
<%= p.input :address %>
<%= p.input :image %>
<% end %>
<%= p.buttons %>
<% end %>

When I load the form in my browser, I see the following error:

undefined method `place_id' for #

Here's the kicker: in my Place model, if I change `has_one :image` to `has_many :images` and `accepts_nested_attributes_for :image` to `accepts_nested_attributes_for :images`, and in my form I change `p.input :image` to `p.input :images`, then the error disappears and Formtastic properly renders a multiselect input element containing all available image objects. So why am I getting this error when I use a `has_one` association rather than seeing a select input element?