Jump to content

The ultimate community for Ruby on Rails developers.


Rails 4.1: Nested form gives missing parent id error on submit using paperclip

rails4.1 nested forms paperclip

  • Please log in to reply
1 reply to this topic

#1 Vell



  • Members
  • 83 posts
  • LocationWashington, DC

Posted 16 May 2014 - 12:35 PM

Hello all,


I can't seem to figure out what is wrong with my nested form where on submission it complains that the parent id is missing from from the child record (if I made sense). This is my first time building a nested form and I have been strugging with this for a little over a day.


Basically, I would like to allow the user to upload attachments at the same time they submit a discussion form but can't seem to figure out get the ID in the submission.


The error I get is that the parent can't be blank (the parent id is missing)


I have tried adding a hidden field in the nested form but that didn't seem to work either. I am wondering if anyone can help me understand what I might be doing wrong.


Here is what I currently have in my application:



def new
  @discussion = Discussion.new
  @discussion_group = DiscussionGroup.find(params[:discussion_group_id])
  @categories = DiscussionCategory.all.collect{|category| [category.name, category.id]}

def discussion_params
  params.require(:discussion).permit(:discussion_group_id, :discussion_category_id, :subject, :body, discussion_attachments_attributes: [:id, :name, :_destroy])


has_many :discussion_attachments, :dependent => :delete_all
accepts_nested_attributes_for :discussion_attachments, allow_destroy: true


belongs_to :discussion
  has_attached_file :document
  validates_attachment :document, content_type: {content_type: ALLOWABLE_APPLICATIONS + ALLOWABLE_IMAGES}
 validates :discussion_id, :document_file_name, presence: :true


<%= form_for @discussion, url: admin_discussions_path, method: :post do |f| %>
    <%= f.fields_for :discussion_attachments do |builder| %>
            <% if builder.object.new_record? %>
                <div class='field'>
                    <%= builder.hidden_field :discussion_id, value: @discussion.id %>
                    <%= builder.file_field :document %>
                <% end %>
        <% end %>
  <div class="actions">
    <%= f.submit %>
<% end %>

#2 Vell



  • Members
  • 83 posts
  • LocationWashington, DC

Posted 16 May 2014 - 04:17 PM

Weird enough, everything seems to work fine if I generate a new application and do just that scenario. Very curious why this doesn't work or what is conflicting with it.


I actually think because I had multiple child records being displayed, if I only uploaded 1 file and left the other one empty that this is why I may have been getting the parent id issue.  Adding the bolded seemed to help with that error:

accepts_nested_attributes_for :discussion_attachments, :reject_if => lambda { |a| a[:document_file_name].blank? }

But still my file doesn't upload to the system. It creates the discussion without any issue but no child file is created and no errors are thrown.

Also tagged with one or more of these keywords: rails4.1, nested forms, paperclip

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users