Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formtastic form throws "undefined method" error with has_one association

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 #<Place:0xb801744>

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?

like image 399
Brian D'Astous Avatar asked Feb 12 '26 00:02

Brian D'Astous


1 Answers

According to https://github.com/gregbell/active_admin/issues/575 this is a bug between formtastic and active admin - I suggest you follow the issue on github and also explain your scenario to them, it might prompt a fix, if more than one community member reports the issue.

like image 182
Shaun Wilde Avatar answered Feb 15 '26 18:02

Shaun Wilde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!