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

Closed
pailoro opened this Issue Jul 12, 2012 · 8 comments

Comments

Projects
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?

Thanks!

Pailoro,
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
end

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
end

rules/_form.html.erb

<%= 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>
      <ul>
      <% @rule.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% 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 %>
  </div>

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

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

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

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

rules/_figure_fields.erb

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

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.

Thorn731,
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
end

pailoro commented Jul 25, 2012

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

Owner

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

Owner

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