1

I'm trying to write an ActiveAdmin form for my Gallery model that has a HABTM relationship with my Image model.

Note that this is not a duplicate of this question, as I am already using "accepts_nested_attributes_for" in my Gallery model.

Here is my Gallery form that throws the error:

<% @gallery.images.build %>
<%= semantic_form_for [:admin, @gallery] do |g| %>
  <%= g.inputs "Details" do %>
    <%= g.input :title %>
    <%= g.input :images, :as => :check_boxes, :label_method => Proc.new { |image| image_tag(image.thumb_path, :alt => "") + content_tag("h3", image.title)  } %>
  <% end %>
  <%= g.inputs :for => :images, :name => "New Image" do |image| %>
    <% if image.new_record? %>
      <%= image.input :title %>
      <%= image.input :asset, :as => :file %>
    <% end %>
  <% end %>
  <%= g.buttons %>
<% end %>

Here is my Gallery model:

class Gallery < ActiveRecord::Base
    belongs_to :admin_user
    has_and_belongs_to_many :images
    accepts_nested_attributes_for :images
end

Here is my Image model:

class Image < ActiveRecord::Base
   belongs_to :admin_user   
   has_and_belongs_to_many :galleries

   has_attached_file :asset, :styles => {  
                                      :thumb => "96x96#"
                                      }

   validates_attachment_presence :asset
end

Here is the error log:

NoMethodError in Admin/galleries#edit

Showing /home/***/www/***/app/views/admin/galleries/_form.html.erb where line #8 raised:

undefined method `new_record?' for #<Formtastic::SemanticFormBuilder:0x164b2088>

Extracted source (around line #8):

5:       <%= g.input :images, :as => :check_boxes, :label_method => Proc.new { |image| image_tag(image.thumb_path, :alt => "") + content_tag("h3", image.title)  } %>
6:     <% end %>
7:     <%= g.inputs :for => :images, :name => "New Image" do |image| %>
8:       <% if image.new_record? %>
9:         <%= image.input :title %>
10:         <%= image.input :asset, :as => :file %>
11:       <% end %>

Trace of template inclusion: /home/bdastous/.rvm/gems/ruby-1.9.2-p290@rails31/gems/activeadmin-0.3.1/app/views/active_admin/resource/edit.html.arb

Rails.root: /home/bdastous/www/hotel_app_cms
Application Trace | Framework Trace | Full Trace

app/views/admin/galleries/_form.html.erb:8:in `block (2 levels) in _app_views_admin_galleries__form_html_erb__372176781_187575760'
app/views/admin/galleries/_form.html.erb:7:in `block in _app_views_admin_galleries__form_html_erb__372176781_187575760'
app/views/admin/galleries/_form.html.erb:2:in `_app_views_admin_galleries__form_html_erb__372176781_187575760'
Community
  • 1
  • 1
Brian D'Astous
  • 1,314
  • 1
  • 18
  • 26

2 Answers2

3
<% if image.object.new_record? %>
Chap
  • 3,483
  • 1
  • 24
  • 27
1

I think it would help if you posted the error log dump. It's a bit ambiguous what new_record? it's referring to.

I think the problem has to do with the following lines:

<% @gallery.images.build %>
<%= g.inputs :for => :images, :name => "New Image" do |image| %>
  <% if image.new_record? %>

There are two scenarios that I see:

1) <% if image.new_record? %> is throwing the error because image is nil.
2) <%= g.inputs :for => :images, :name => "New Image" do |image| %> is throwing the error. #inputs is a wrapper around the rails form builder #fields_for method which executes differently whether the object is a new record or not (I think it includes the id in the attributes hash as a hidden value if image.new_record? returns true).

Either way, its because the form builder is trying to create a form for a nil object. You did the right thing in trying to build a gallery image before the #inputs was called, but I think it needs to be even earlier than that. It should probably be placed before the form even starts being built (above the #semantic_form_for).

There are two other similar solutions. You should probably build the gallery in the controller before the form ever begins to be rendered. Then, when you start the form for the @gallery object, it already knows it has an image built. Alternatively, I think it may work if you specify

<%= g.inputs :for => @gallery.images, :name => "New Image" do |image| %>

Hopefully either of those will work because it seems like the problem is that it's not accessing the just-built image.

dcashman
  • 197
  • 10
  • Thanks - I just added the error log. I am now building the gallery image above semantic_form_for, but the error persists. I do intend to refactor later so that the new gallery image is built in the controller. – Brian D'Astous Oct 14 '11 at 20:32