Teetof
Teetof

Reputation: 87

Rails paperclip and document type

in my rails app, users can upload documents (pdf, png , doc ie...) but there are separate into few categories (for business rules). For instance type 1 is "id card", type 2 is "resume"... ie.. Whit paperclip i create model and controller (and db table) for store this documents, and i added a custom fied "typedoc" (integer) I want to set the typedoc field with integer depending on the "type" file uploaded ...

In view, i repeat this below code for each "type" . How can i update the custom field in document table for each type ? How can I pass typedoc var to controller to update ?

Thanks in advance

    <%= form_for @user, url: user_documents_path(@user) , method: 'post', 
    html: {multipart: true} do |f| %> 

                        <div class="row">
                            <div class="col-md-4 form-group">
                                <span class="btn btn-default btn-file">
                                    <i class="fa fa-cloud-upload" aria- 
   hidden="true"></i> Choose doc 1
                                        <%= file_field_tag "documents[]", 
    type: :file, multiple: true %>

                                </span>
                            </div>                                 

                            <div class="col-md-4 body ">    
                                <%= f.submit "Add id card", class: "btn 
    btn-form" %>                                              
                            </div>
                            <div class="col-md-4 form-group" 
    id="documents"><%= render 'documents/documents_list' %></div>
                        </div>
                    <% end %>  

...

Upvotes: 0

Views: 122

Answers (2)

Tom
Tom

Reputation: 1320

My understanding is you have a different submit button for each type? We can see <%= f.submit "Add id card", class: "btn btn-form" %> in the code. Assuming there are also other buttons of the same accord (<%= f.submit "Add avatar", class: "btn btn-form" %> ?) then we will be able to use the params[:commit] field to set the typedoc.

params[:commit] is going to equal the text of the submit button used. We can then, in the controller, map that to the integer needed. eg: params[:commit] = "Add id card" then typedoc = 0.

This will also work if each submit button is on it's own form. The :commit param will still be set.

Upvotes: 0

Anand
Anand

Reputation: 6531

You can pass typedoc with the url for this form

for example user click on upload id_card link then you can pass it as

new_user_document_path(typedoc: 1)

And pass typedoc in form dynamically -

<%= form_for @user, url: user_documents_path(@user) , method: 'post', 
  html: {multipart: true} do |f| %> 
  <div class="row">
    <div class="col-md-4 form-group">
      <span class="btn btn-default btn-file">
      <i class="fa fa-cloud-upload" aria- 
        hidden="true"></i> Choose doc 1
        <%= f.file_field :documents, multiple: true, name: "user[documents][]"%>
      </span>
    </div>
    <!-- set value accordinly if you according to type of document  eg: -type 1 is "id card", type 2 is "resume"... ie. -->
    <%= f.hidden_field :typedoc, value: params[:typedoc]%> 
    <div class="col-md-4 body ">    
      <%= f.submit "Add id card", class: "btn 
        btn-form" %>                                              
    </div>
    <div class="col-md-4 form-group" 
      id="documents"><%= render 'documents/documents_list' %></div>
  </div>
<% end %> 

Upvotes: 1

Related Questions