Not able to get form to submit when using nested_form with paperclip
vmcilwain opened this issue · 2 comments
Hello,
I am trying to get the nested_form gem to work in rails 4.1 for adding attachments using paperclip. Everything renders as expected but when I submit the form nothing happens. No errors, no log output or anything.
I am not sure the best way to debug this problem since I basically don't have anything to go on. Has anyone else has run into this problem or can you give advice for the best way I can attack this?
What I currently have is as follows:
discussions_controller.rb:
def new
@discussion = Discussion.new
@discussion_group = DiscussionGroup.find(params[:discussion_group_id])
@categories = DiscussionCategory.all.collect{|category| [category.name, category.id]}
2.times{@discussion.discussion_attachments.build}
end
def discussion_params
params.require(:discussion).permit(:discussion_group_id, :discussion_category_id, :subject, :body, discussion_attachments_attributes: [:id, :name, :_destroy])
end
discussion.rb
has_many :discussion_attachments, :dependent => :delete_all
accepts_nested_attributes_for :discussion_attachments, allow_destroy: true
discussion_attachment.rb
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
new.html.erb
<%= form_for @discussion, url: admin_discussions_path, method: :post do |f| %>
<%= nested_form_for @discussion do |f| %>
Upload Document
<%= f.fields_for :discussion_attachments do |builder| %>
<div class='field'>
<%= builder.file_field :document %>
<%= builder.link_to_remove "Remove this document" %>
</div>
<% end %>
<%= f.link_to_add "Add a document", :discussion_attachments %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
UPDATE:
The form works as it is supposed to when I uncomment the nested_form_for block. But the moment I uncomment that block but leave the fields_for block on the inside of that commented out this issue shows. This same issue happens in rails 4.0.4.
Just a passer-by... but it looks like you forgot to add :document
to your discussion_attachments_attributes array?
Err, why do you have both form_for
and nested_form_for
? Use one of them, but not both.
nested_form_for
is gonna use the helpers provided by this gem (So you can use fields_for
, link_to_add
, etc..)
form_for
is gonna use Rails default helpers.
<%= nested_form_for @discussion, url: admin_discussions_path, method: :post do |f| do |f| %>