0

In the devise users edit profile page I want to add a custom field called "add verification documents" which takes in all documents uploaded using paperclip gem and has to be updated.A User can upload multiple documents.

verification_document.rb

  class VerificationDocument < ActiveRecord::Base
 belongs_to :user
 has_attached_file :verification_documents                
   validates_attachment_content_type :verification_documents, {:content_type => %w( application/pdf )} 
 end

user.rb

 class User < ActiveRecord::Base
 has_many :verification_documents
 end

In views/devise/registrations/edit.html.erb

   <div class="row">
   <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {method: :put}, multipart: true) do |f| %>
   <div class="form-group">
            <%= f.text_field :fullname, autofocus: true, placeholder: "Full Name *", class: "form-control" %>
          </div>

          <div class="form-group">
            <%= f.email_field :email, placeholder: "Email *", class: "form-control" %>
          </div>
           <span class="btn btn-default  btn-file btn-green pad_small">Upload Verification Documents
                <input type="file" accept="application/pdf" name="input-file-preview"/>  
                  <%= file_field_tag "verification_documents[]", type: :file, multiple: true %>
                </span> 
     <div class="actions">
            <%= f.button "Save", class: "btn btn-green pad_small" %>
          </div>

routes.rb

      devise_for :users,
         path: '',
         path_names: {sign_in: 'login', sign_out: 'logout',sign_up: 'sign-up', edit: 'profile'},
         controllers: {
             omniauth_callbacks: 'omniauth_callbacks',
             registrations: 'registrations'
         }

How can I make update action in RegistrationsController < Devise::RegistrationsController as :

      def update
      if @user.update(user_params)
    if params[:verification_documents]
    params[:verification_documents].each do |doc|
      @user.verification_documents.create(verification_documents: doc)
    end
   end
   redirect_to root_path, notice: "Updated..."
   else
   render :edit
   end

How to upload multiple verification_documents all at a time during the devise user edit action?

Note: I have successfully uploaded one verification_document in the devise users table using the paperclip gem.But I want to upload multiple verification_documents and show all the documents in the users profile view page.

I added the below lines in ApplicationController for single document upload:

   def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname, :subscribe])
devise_parameter_sanitizer.permit(:account_update, keys: [:fullname, :reset_password_token, :verification_documents])

 end

My Requirement is very similar to Rails Devise - Register User with Associated Model , But Instead of address attributes, here I want to upload multiple verification verification documents to the verification_documents model while updating the devise user.

Any Help is Highly Appreciated.Thanks in Advance!

Community
  • 1
  • 1
current_user
  • 1,172
  • 1
  • 16
  • 28
  • use _accepts_nested_attributes_for_ in user model and in your _registrations/edit.html.erb_ file use _fields_for_ to upload multiple file. Please refer http://stackoverflow.com/questions/13506735/rails-has-many-through-nested-form#answer-21060278 – Prashant Vardhan Singh Apr 05 '17 at 12:13
  • for multiple file upload use need to use nested form ref: http://stackoverflow.com/questions/24597721/rails-4-multiple-image-upload-using-paperclip – Dnyanarth lonkar Apr 05 '17 at 12:17

1 Answers1

0

Add multipart: true in HTML block of form_tag.

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {method: :put, multipart: true}) do |f| %>

Also on browser inspect the code and see what attributes you can see in <form> tag.

If you are able to add one file but facing problem while uploading multiple files then most probably reason is rails could not add multipart: true in form tag

bunty
  • 1,068
  • 6
  • 17