0

Good day,

I have a devise users model and in edit user page I want to add a custom field called "add verification documents" which takes in multiple documents(pdf's) that are uploaded in edit user page. A User can upload multiple documents.

# user.rb

class User < ActiveRecord::Base
  has_many :verification_documents
end

# 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'
           }

My Requirement is 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 during update action of the devise user model.

# 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>
</div>

# 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

Is it possible to update something like below in RegistrationsController as ?

# RegistrationsController < Devise::RegistrationsController

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
end

In application_controller.rb I have,

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

Any Help is Highly Appreciated.Thanks in Advance!

UPDATE 1

I have added this to the Registrations Controller

def update
 super

   @user = User.find(current_user.id)

    if params[:user][:verification_documents]
    params[:user][:verification_documents].each do |doc|
    @user.verification_documents.create(verification_documents: doc)
  end

 puts "document saved"
 else
puts "not saved"
end
end

I tried to change the user phone number and upload the verification_documents (pdf's) in the users edit page.The values of devise user model Attribute's were successfully updated(The phone number is an attribute of user model).But the values for the verification_document model attributes are not saved. phone number is updated but the verification_documents were still not saved

Log:

    Processing by RegistrationsController#update as HTML
      Parameters: {"utf8"=>"▒~\~S", "authenticity_token"=>"Xic+IFWWdzK4KaFzgnVzsOjMDPDaznJBjosj69khn3AfhrJr0mWsJrobK/mWHWiaANFqQAKj7wUPRYslfJZoPw==", "user"=>{"fullname"=>"Mohan Krishna Gangarapu", "email"=>"mohankrish93@gmail.com", "alternate_email"=>"", "business_name"=>"", "location"=>"", "phone_number"=>"9999999999", "alternate_phone_number"=>"", "dealer"=>"0", "fleet_manager"=>"0", "description"=>"", "url"=>"", "current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "deactivate"=>"false"}, "verification_documents"=>[#<ActionDispatch::Http::UploadedFile:0x007f1412365420 @tempfile=#<Tempfile:/tmp/RackMultipart20170411-23997-11lrzwz.pdf>, @original_filename="pdf-test.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"verification_documents[]\"; filename=\"pdf-test.pdf\"\r\nContent-Type: application/pdf\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f14123653f8 @tempfile=#<Tempfile:/tmp/RackMultipart20170411-23997-e1grsp.pdf>, @original_filename="pdfurl-guide.pdf.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"verification_documents[]\"; filename=\"pdfurl-guide.pdf.pdf\"\r\nContent-Type: application/pdf\r\n">], "button"=>""}
      ^[[1m^[[36mUser Load (0.2ms)^[[0m  ^[[1mSELECT  `users`.* FROM `users` WHERE `users`.`id` = ?  ORDER BY `users`.`id` ASC LIMIT 1^[[0m  [["id", 686]]
      ^[[1m^[[35mUser Load (0.4ms)^[[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT 1  [["id", 686]]
    Unpermitted parameters: business_name, location
      ^[[1m^[[36mSQL (0.1ms)^[[0m  ^[[1mBEGIN^[[0m
      ^[[1m^[[35mSQL (0.3ms)^[[0m  UPDATE `users` SET `phone_number` = ?, `deactivate` = ?, `updated_at` = ? WHERE `users`.`id` = ?  [["phone_number", "9999999999"], ["deactivate", 0], ["updated_at", "2017-04-11 05:32:57"], ["id", 686]]
      ^[[1m^[[36m (1.8ms)^[[0m  ^[[1mCOMMIT^[[0m
    Redirected to http://104.251.212.100:3000/
    Completed 302 Found in 17ms (ActiveRecord: 3.6ms)


    Started GET "/" for 156.73.67.6 at 2017-04-11 05:32:57 -0500
    Cannot render console from 156.73.67.6! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
    Processing by PagesController#home as HTML
Community
  • 1
  • 1
current_user
  • 1,172
  • 1
  • 16
  • 28
  • Can you please explain what the actual problem is. As far as I can see your code should work. See [Rails Guides](http://guides.rubyonrails.org/association_basics.html#has-many-association-reference) for documentation on `create`. Maybe you should do something about invalid `verification_documents` returned by `@user.verification_documents.create`. – Ray Wojciechowski Apr 11 '17 at 08:35
  • @Ray Wojciechowski , I have updated the log and code – current_user Apr 11 '17 at 12:11
  • `verification_documents` is not part of the `user` part of your params hash. You need to access it via `params[:verification_documents]`. Apart from this I don't know what the problem is. Please explain it further. – Ray Wojciechowski Apr 11 '17 at 13:50

0 Answers0