使用回形针上传简历

时间:2014-02-22 12:23:32

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

我正在使用paperclip gem来允许用户上传他们的简历,问题是用户点击上传后。正如一些教程中所提到的,我设置了回形针env,以便它在开发中获得路径,但仍然没有上传。

#Error that gets showed on browser 
RuntimeError in ProfilesController#update_resume

#profilescontroller#update_resume
def update_resume
    update! :notice => "Resume successfully updated!" do |success, failure|
      success.html {
          if !@user.section_layout.nil?
            ResumeSection.delete_all "user_id = #{@user.id}"
            @user.section_layout.each {|key,value|
              rs = ResumeSection.new(:section_name => key.encode("UTF-8"), :html => value.encode("UTF-8"), :user => @user)
              rs.save
            }
            redirect_to profile_path
          end
      }
      failure.html { render :template => "/profiles/edit/resume" }
    end
  end


#These are the logs after
Started POST "/profile/update_resume" for 127.0.0.1 at 2014-02-22 17:36:18 +0530
  Processing by ProfilesController#update_resume as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zRlPQPI88Eqgko5UUdR2YUvmMHsjSDKS0sqOOj2kTu0=", "user"=>{"rb_resume"=>#<ActionDispatch::Http::UploadedFile:0xaef29b4 @original_filename="resume .odt", @content_type="application/vnd.oasis.opendocument.text", @headers="Content-Disposition: form-data; name=\"user[rb_resume]\"; filename=\"resume .odt\"\r\nContent-Type: application/vnd.oasis.opendocument.text\r\n", @tempfile=#<File:/tmp/RackMultipart20140222-16462-1mvioqy>>}, "commit"=>"Update Resume"}
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1
  SQL (0.2ms)  BEGIN
Command :: file -b --mime-type '/tmp/a495763e62ffd9dac862588d08a1ed5420140222-16462-6djcd4'
  SQL (0.1ms)  ROLLBACK
Completed   in 69ms

RuntimeError (Input file needs to be pdf or word format):
  lib/resume/resume_builder.rb:81:in `convert_to_html'
  lib/resume/resume_builder.rb:17:in `initialize'
  lib/paperclip/resume_builder.rb:10:in `new'
  lib/paperclip/resume_builder.rb:10:in `make'
  app/controllers/profiles_controller.rb:51:in `update_resume'

Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (257.7ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (264.2ms)

更新 - 应用程序跟踪

lib/resume/resume_builder.rb:81:in `convert_to_html'
lib/resume/resume_builder.rb:17:in `initialize'
lib/paperclip/resume_builder.rb:10:in `new'
lib/paperclip/resume_builder.rb:10:in `make'
app/controllers/profiles_controller.rb:51:in `update_resume'

欢迎任何有关正确行事的建议:)

1 个答案:

答案 0 :(得分:1)

错误

这是你的问题 -

RuntimeError (Input file needs to be pdf or word format)

我相信这一行是问题所在:

ResumeSection.new(:section_name => key.encode("UTF-8"), :html => value.encode("UTF-8"), :user => @user)

<强>验证

我确保Paperclip仅允许type validation of Word or PDF

validates_attachment_content_type :resume, :content_type => ["application/pdf","application/vnd.ms-excel",     
         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
         "application/msword", 
         "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
         "text/plain"]

如果你这样做,你能告诉我们会发生什么吗?