gregbell/active_admin — Issue #889: Nested form for has_one/belongs_to doesn't work
Author: rocksolidwebdesign
Created: 2012-01-03 09:13:24+00:00
State: open

I can use f.has_many but I can't use a nested subform for a has_one or belongs_to relationship

I get this error

```
undefined method `html_safe' for nil:NilClass
```

here's the stack trace https://gist.github.com/0f402835e3a111e10e5f

here's my code

```ruby
# app/models/image.rb
class Image < ActiveRecord::Base
end

# app/models/product_image.rb
class ProductImage < ActiveRecord::Base
belongs_to :product
belongs_to :color
belongs_to :image
accepts_nested_attributes_for :image
end

# app/admin/product_images.rb
ActiveAdmin.register ProductImage do
form do |f|
f.inputs "General" do
f.input :product
f.input :color
end

f.inputs :name => "Image", :for => :image do |img_form|
img_form.input :title
end

f.buttons
end
end
```

my image model has nothing more than two string columns, :title and :description - eventually the image model will have paperclip attachments for example, but for now I'm trying to keep it simple. the product_image model has only three columns, product_id, color_id and image_id

choosing a color from a dropdown makes sense, but I will always want the user to have the image model editable inline, creating a separate image model first and then navigating BACK to the product_image model to select the newly created image from a dropdown doesn't fit the spec

I tried some examples from this (now closed) issue but got the same errors as this guy https://github.com/gregbell/active_admin/issues/59#issuecomment-1911627 (no errors but just a blank section where the nested form fields should be) and the issue has been closed without anyone responding to him

after some cursory checking, it seems this is possibly related to Formtastic because it's happening in the yield of https://github.com/gregbell/active_admin/blob/master/lib/active_admin/form_builder.rb#L118 which is yielding to super which is Formtastic - but that doesn't mean that ActiveAdmin is passing the correct stuff to Formtastic

Formtastic does claim that it handles nested one to one relationships just fine as the very first example from the README indicates https://github.com/justinfrench/formtastic#readme

their example reads, and as far as I can tell, the syntax of my code is identical

```
<%= f.inputs :name => "Author", :for => :author do |author_form| %>
<%= author_form.input :first_name %>
<%= author_form.input :last_name %>
<% end %>
```

also I have tried using both belongs_to and has_one on the product_image model, but I get the same error either way