Rails设计 - 使用关联模型更新用户

时间:2017-04-11 06:43:43

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

美好的一天,

我有一个设计用户模型,在编辑用户页面中,我想添加一个名为“添加验证文档”的自定义字段,该字段包含在编辑用户页面中上传的多个文档(pdf)。用户可以上传多个文档。

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

我的要求类似于 Rails Devise - Register User with Associated Model, 但是,在此处,我想在设计用户模型的更新操作期间将多个验证验证文档上载到verification_documents模型,而不是地址属性。

# 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

是否可以在RegistrationsController中更新如下所示的内容?

# 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

我有application_controller.rb

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

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

更新1

我已将此添加到注册控制器

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

我尝试更改用户电话号码并在用户编辑页面上传了verification_documents(pdf's)。设计用户模型属性的值已成功更新(电话号码是用户模型的属性)。但是不保存verification_document模型属性。 电话号码已更新,但仍未保存verification_documents

日志:

    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

0 个答案:

没有答案