覆盖Devise用户的更新操作

时间:2017-04-05 11:48:33

标签: ruby-on-rails ruby ruby-on-rails-4 devise

在设计用户编辑个人资料页面中,我想添加一个名为&#34的自定义字段;添加验证文件"它接收使用paperclip gem上传的所有文件,并且必须更新。用户可以上传多个文件。

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

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

如何在RegistrationsController中进行更新操作&lt;设计:: 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

如何在设计用户编辑操作期间一次上传多个verification_documents?

注意:我已使用paperclip gem在devise用户表中成功上传了一个verification_document。但我想上传多个verification_documents并在用户个人资料视图页面中显示所有文档。

我在ApplicationController中为单个文档上传添加了以下行:

   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

我的要求非常相似 Rails Devise - Register User with Associated Model, 但是,在此处,我希望在更新设计用户时,将多个验证验证文档上传到verification_documents模型,而不是地址属性。

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

在form_tag的HTML块中添加multipart: true

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

同样在浏览器上检查代码并查看您可以在<form>标记中看到的属性。

如果您能够添加一个文件但在上传多个文件时遇到问题,那么很可能是因为rails无法在表单标记中添加multipart:true