thoughtbot / paperclip

Easy file attachment management for ActiveRecord
https://thoughtbot.com
Other
9.01k stars 2.43k forks source link

validates attachment filename throws error. #1661

Closed chitrank-samaiya closed 9 years ago

chitrank-samaiya commented 10 years ago

Hi Guys, I am trying to implement validation for filename, but it is giving undefined method 'before_file_name_post_process'.

Here is my model code:

    class JobSeeker
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Paperclip
        include SimpleEnum::Mongoid
        include Mongoid::Search

         # attributes
         field :first_name, type: String
         field :last_name, type: String
         field :current_title, type: String
         field :summary, type: String
         field :phone, type: String
         field :zipcode, type: String
         field :resume_data, type: String
         field :applied_jobs, type: Array, default: []

        #paperclip upload
        has_mongoid_attached_file :resume,
        :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
        :url => "/system/:attachment/:id/:style/:filename"
        validates_attachment_file_name :resume_file_name, :matches => [/pdf\Z/]

        search_in :resume_data, :skill => [:primary, :secondary]

        #callbacks
        after_save :parse_attachment

        def parse_attachment
            if self.resume.exists?
                file = self.resume.path
                    if self.resume_content_type == 'application/pdf'
                          reader = PDF::Reader.new(file)
                          data = reader.pages.each do |page|
                          page.text
                        end
                        data = data.join("\n")
                    else  
                        MSWordDoc::Extractor.load(file) do |doc|
                          data = doc.document
                        end
                    end
                data = data.squeeze rescue nil
                self.set(resume_data: data)
            end  
        end 
    end

Error:

NoMethodError: undefined method `before_resume_file_name_post_process' for JobSeeker:Class
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/paperclip-4.2.0/lib/paperclip/validators.rb:67:in `create_validating_before_filter'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/paperclip-4.2.0/lib/paperclip/validators.rb:60:in `block in validate_before_processing'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/paperclip-4.2.0/lib/paperclip/validators.rb:58:in `each'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/paperclip-4.2.0/lib/paperclip/validators.rb:58:in `validate_before_processing'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/paperclip-4.2.0/lib/paperclip/validators/attachment_file_name_validator.rb:75:in `validates_attachment_file_name'
    from /home/chitrank/local/PrihireApi/app/models/job_seeker.rb:31:in `<class:JobSeeker>'
    from /home/chitrank/local/PrihireApi/app/models/job_seeker.rb:1:in `<top (required)>'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:443:in `load'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:443:in `block in load_file'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:633:in `new_constants_in'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:442:in `load_file'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:342:in `require_or_load'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:480:in `load_missing_constant'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:180:in `const_missing'
    from (irb):10
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/chitrank/.rvm/gems/ruby-2.1-head@prihire_api/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
tute commented 9 years ago

Hi @chitrank-samaiya. Is this still an issue for you? Can you upgrade to 4.2.1 and see if the problem persists? Thanks for your input.

chitrank-samaiya commented 9 years ago

Thanks @tute. This is no more issue for me.