How do you modify the contents of a field in the same situation of nested attributes?

advertisements

I am using paperclip and want to be able to modify the contents of a caption field in the attachments file.

    .. documents\edit.html.erb

<h1>Edit Document</h1>
Document id: <%= @document.id %>

<%= render 'edit_form' %>

<hr>
<b>Attachments:</b>  Any document attachments will be shown below.
<hr>
<% @attachments.each do |attachment| %>

        <% if  attachment.image_content_type.include? "image"  %>
            <%= link_to image_tag( attachment.image.url,    height: '132'), attachment.image.url, :target => "_blank" %><br>
        <% end %>
        <%= link_to attachment.image.url, attachment.image.url,:target => "_blank" %> <br>

        Caption/Summary:  <%= attachment.caption %>  <br>

        Attachment type:  <%= attachment.image_content_type %> <br>
        Updated at     :  <%= attachment.image_updated_at %> <br>
        File size: <%= attachment.image_file_size %> <br>

        <%= @confirm_message = 'Delete this attachment? ' + attachment.image.url %>
        <%= button_to 'Delete this attachment', attachment, method: :delete, data: { confirm: @confirm_message }%>
        <hr>
    <% end %>

The above code will show all of the attachments for a particular document. The captions line will show the contents of the caption field for that particular attachment (I manually entered the captions in the database)

What I can't figure out is how to let the user change the caption for an attachment. I think I need a submit button with a text field for the caption. If I put a text_field or text_field_tag and hit submit (which includes the document form) the caption field does not get updated.

I included caption in the params for my attachments controller

def attachment_params
      params.require(:attachment).permit(:caption,
                                         :file_type_string,
                                         :document_id,
                                         :document_token,
                                          :caption)
    end

But not for the document controller. Does it have to go there as perhaps document.caption?

 def document_params
      params.require(:document).permit(:subject,
                                       :body,
                                       :category_id,
                                       :tag_id,
                                       :author_id,
                                       :reviewer_id,
                                       etc..
                                       :company,
                                       :request_type,
                                       )

    end


Your question is too broad and need multiple file changes to work as expected.

You have added permit_params in proper place. But the form you are posting is belong to document not attachment. So that can't get updated.

You need to nested form to do so. Currently you have messed up with things. You need to make changes from scratch for this.

In this asscicast You can see similar implementation. But it's not for rails 4. To make it work with rails 4 here are some instructions.