why when uploading a file using carrierwave in rai

2019-09-02 09:22发布

问题:

I am unsure why i am getting the below error. I am new to rails and any help and advise would be much appreciated.

  1. i am creating a recruitment site
  2. i am trying to allow jobseekers to upload their profile image which works successfully
  3. now, i am trying to allow job seekers to upload their CVs
  4. when i try to upload a CV and save i get the below error
  5. i've followed the step of carrierwave but i still can't seem to see where i'm going wrong
  6. the error prompts up when i try to upload pdf files or doc file, uploading jpg files works fine

any help or advise would be much appreciated.

error message in terminal:

rollback transaction

Started PATCH "/userjs/1" for 127.0.0.1 at 2015-06-21 20:33:44 +0100
Processing by UserjsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"KosfL1GqfZM03PFWb2aMuIKKlgPcMLSkITI9VIbjKcA=", "userj"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb75c2d5f58 @tempfile=#<Tempfile:/var/folders/72/z21dh3tx6jb03dscv7m3wc_h0000gn/T/RackMultipart20150621-9939-7rafcv>, @original_filename="profileimage.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"userj[image]\"; filename=\"profileimage.jpg\"\r\nContent-Type: image/jpeg\r\n">, "cvattachment"=>#<ActionDispatch::Http::UploadedFile:0x007fb75c2d5e90 @tempfile=#<Tempfile:/var/folders/72/z21dh3tx6jb03dscv7m3wc_h0000gn/T/RackMultipart20150621-9939-uh56yj>, @original_filename="00. Resume - A. Richill Tamakloe.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"userj[cvattachment]\"; filename=\"00. Resume - A. Richill Tamakloe.pdf\"\r\nContent-Type: application/pdf\r\n">, "firstname"=>"aknorbea", "lastname"=>"artloe", "city"=>"accra", "language"=>"french | gary carr"}, "commit"=>"Update Userj", "id"=>"1"}
  Userj Load (0.1ms)  SELECT  "userjs".* FROM "userjs"  WHERE "userjs"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendered userjs/_form.html.erb (10.3ms)
  Rendered userjs/edit.html.erb within layouts/application (11.5ms)
Completed 200 OK in 554ms (Views: 539.4ms | ActiveRecord: 0.2ms)

uploaders/cvattachment.rb

# encoding: utf-8
class CvattachmentUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20150621190355) do
  create_table "userjs", force: true do |t|
    t.string   "email",                     default: "", null: false
    t.string   "encrypted_password",        default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",             default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "firstname"
    t.string   "lastname"
    t.integer  "number"
    t.string   "city"
    t.string   "image"
    t.string   "cvattachment"
  end

  add_index "userjs", ["email"], name: "index_userjs_on_email", unique: true
  add_index "userjs", ["reset_password_token"], name: "index_userjs_on_reset_password_token", unique: true

end

userj.rb

class Userj < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :adverts, through: :forms
  has_many :forms

  mount_uploader :image, ImageUploader
  mount_uploader :cvattachment, CvattachmentUploader
end

views/userjs/_form.html.erb

<%= simple_form_for(@userj) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    image    <%= f.file_field :image %>
    cv       <%= f.file_field :cvattachment %>
    <%= f.input :firstname %>
    <%= f.input :lastname %>
    <%= f.input :city %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

views/userjs/show.html.erb

<div>
  <p>
    <strong>profile image:</strong>
    <div><%= image_tag(@userj.image_url) %></div>
  </p>

  <p>
    <strong>cv:</strong>
    <div><%= image_tag(@userj.cvattachment) %></div>
  </p>

  <div>
  <%= link_to 'Edit', edit_userj_path(@userj) %> |
  <%= link_to 'Back', userjs_path %>
  </div>

  <%= render 'shared/footer_jobseeker' %>
</div>

回答1:

Comment these lines from CvattachmentUploader

def extension_white_list
  %w(jpg jpeg gif png)
end

Restart server and try again.