Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Do not work with nested forms #786

pailoro opened this Issue Jul 12, 2012 · 8 comments


None yet
5 participants

pailoro commented Jul 12, 2012

Hi, I have a nested form who work with two models. One of this is a Model for upload files. The form work fine, also the fields nesteds, but when I selec a file for upload, I got the error:

NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "attachments" ("attachable_id", "attachable_type", "created_at", "description", "file", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)

Just the image filed generate this error. 'm sure is a problem with carrierwave.

Someone else have the same situation?


I am using nested forms also and have the same issue, same error.

Form displays fine but, when saving the form with a new image i get the same error. NoMethodError: undefined method `name' for nil:NilClass:...

If you need anymore info please let me know.

rule model

class Rule < ActiveRecord::Base
  attr_accessible :rule_description, :rule_number, :sections_attributes, :notes_attributes, :figures_attributes

  has_many  :sections
  accepts_nested_attributes_for :sections,  allow_destroy: true

  has_many  :notes, :as => :notable
  accepts_nested_attributes_for :notes, allow_destroy: true

  has_many  :figures, :as => :figurable
  accepts_nested_attributes_for :figures, allow_destroy: true

  default_scope :order => :rule_number

Figure Model

class Figure < ActiveRecord::Base
  attr_accessible :image, :figure_name, :figure_note, :figure_order
  default_scope :order => :figure_order

  belongs_to :figurable, polymorphic => true

  mount_uploader :image, ImageUploader


<%= form_for @rule, :html => {:multipart => true} do |f| %>
  <% if @rule.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@rule.errors.count, "error") %> prohibited this rule from being saved:</h2>
      <% @rule.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
  <% end %>

  <div class="field">
    <%= f.label :rule_number, 'Rule#' -%> <%= f.text_field :rule_number, :size => 3 %>
    <%= f.label :rule_description ,'Title' -%>  <%= f.text_field :rule_description %>

  <%= f.fields_for :sections do |builder| %>
    <%= render 'section_fields', f: builder  %>
  <% end %>

  <%= f.fields_for :figures do |builder| %>
    <%= render 'figure_fields', f: builder %>
  <% end %>

  <%= link_to_add_fields 'Add a figures',       f,  :figures %>
  <%= link_to_add_fields 'add a new section', f, :sections %>

  <div class="actions">
    <%= f.submit %>
<% end %>


<fieldset id="fieldFigure">
    <%= f.file_field :image %>
    <%= f.check_box :_destroy %>
    <%= f.label :_destroy, "Remove figure" %>
      <td><%= f.label :figure_name, "Figure Name:" %></td>
      <td><%= f.text_field :figure_name %></td>
      <td><%= f.label :figure_note, "Figure Note:" %></td>
      <td><%= f.text_field :figure_note %></td>
      <td><%= f.label :figure_order, "Figure Order:" %></td>
      <td><%= f.text_field :figure_order, :size => 1 %></td>

thorn commented Jul 24, 2012

I can advise you to use simple_form instead regular form builder, it works fine with carrierwave and nested fileds. Try just to replace form_for to simple_form_for and leave all other fields alone.

Thanks for the tip. However; I still get the same error.

I'm not sure if the order in which mount_uploader appears makes a difference however, In my situation I moved it just below the attribute accessible and my image saved.

my figure model now looks like the following.

class Figure < ActiveRecord::Base
  attr_accessible :image, :figure_name, :figure_note, :figure_order
  mount_uploader :image, ImageUploader

  belongs_to :figurable, polymorphic => true
  default_scope :order => :figure_order

pailoro commented Jul 25, 2012

I'm already using SimpleForm + NestedForms and still getting the same error =/


bensie commented Jul 29, 2012

I've seen a few other reports of issues with nested forms. We would welcome a pull request if someone wants to work on it. In the meantime it should be considered a known issue.

@bensie bensie closed this Jul 29, 2012

I actually found this to work using Carrierwave, nested_forms & polymorphism: https://gist.github.com/978371


bensie commented Nov 13, 2012

Interesting. I've not actually tried it myself, but glad to hear it worked for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment