0

I have a contact form with a file attachment field that works perfectly fine on Rails 4.0.5 but give me the above error on Rails 4.2.

My aim is to allow customer to upload an image through the form and the image gets attached to the admin sent to the website admin. I am using paperclip to manage uploads.

View

= simple_form_for @inquiry, multipart: true do |f|
  = f.input :spam, as: :hidden
  = f.input :first_name
  = f.input :last_name
  = f.input :email
  = f.input :phone 
  = f.input :image, as: :file
  = f.button :submit, 'Send'

Mailer

class InquiryMailer < ActionMailer::Base
    default from: "foo1@foo.co.uk"

    def admin(inquiry)
        @inquiry = inquiry
        mail to: "foo@foo.co.uk", subject: "Foo Website Inquiry"
        if inquiry.image
          attachment_name = inquiry.image.original_filename
          attachments[attachment_name] = inquiry.image.read
       end
    end
end

Controller

class InquiriesController < ApplicationController

  def new
    @inquiry = Inquiry.new
  end

  def create
    redirect_to new_inquiry_path and return if params[:spam].present?
    @inquiry = Inquiry.new(inquiry_params)
    if @inquiry.valid?
      InquiryMailer.admin(@inquiry).deliver_now
      redirect_to new_inquiry_path
      flash[:success] = "Thank you for contacting Kitchen Worktops Surrey, we will be in touch."
    else
      flash[:error] = "Please correct the below errors and re-send"
      render :new
    end
  end

  private

  def inquiry_params
    params.require(:inquiry).permit(:first_name, :last_name, :email, :phone, :message, :image, :spam)
  end

end

Model

class Inquiry < ActiveRecord::Base

  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :email, presence: true
  validates :message, presence: true

end

Paperclip.rb

Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com'
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = ':class/:attachment/:style/:filename'
Paperclip::Attachment.default_options[:s3_protocol] = 'http'
Paperclip::Attachment.default_options[:s3_credentials] = {
  :bucket => 'foo',
  :access_key_id => 'XXX',
  :secret_access_key => 'XXX' 
}
Stacktrace
Processing by InquiriesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"nIy1mFEtqPIntG4k7o/THZUbKbBuOeN/YqOTFSYopYz2Jnj3aE+O27xzGi8RzInxnpe4UmG8IgxaxuQpJBRkBA==", "inquiry"=>{"spam"=>"", "first_name"=>"John", "last_name"=>"Smith", "email"=>"jsmith@gmail.com", "phone"=>"012324534", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fa30fe757e8 @tempfile=#<Tempfile:/var/folders/vf/gjgb_xyx4g1c1z89vtgb47t40000gn/T/RackMultipart20150218-26958-1v263dp.JPG>, @original_filename="CA-01C_Package_1000px.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"inquiry[image]\"; filename=\"CA-01C_Package_1000px.JPG\"\r\nContent-Type: image/jpeg\r\n">, "message"=>"testing message field"}, "commit"=>"Send"}
  Rendered inquiry_mailer/admin.text.erb (0.1ms)

InquiryMailer#admin: processed outbound mail in 18.1ms
Completed 500 Internal Server Error in 24ms

NoMethodError - undefined method `original_filename' for "#<ActionDispatch::Http::UploadedFile:0x007fa30fe757e8>":String:
  app/mailers/inquiry_mailer.rb:8:in `admin'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionmailer (4.2.0) lib/action_mailer/base.rb:596:in `block in process'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionmailer (4.2.0) lib/action_mailer/base.rb:593:in `process'
  actionmailer (4.2.0) lib/action_mailer/base.rb:584:in `initialize'
  actionmailer (4.2.0) lib/action_mailer/message_delivery.rb:25:in `__getobj__'
  actionmailer (4.2.0) lib/action_mailer/message_delivery.rb:34:in `message'
  actionmailer (4.2.0) lib/action_mailer/message_delivery.rb:85:in `deliver_now'
  app/controllers/inquiries_controller.rb:14:in `create'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  rack (1.6.0) lib/rack/etag.rb:24:in `call'
  rack (1.6.0) lib/rack/conditionalget.rb:38:in `call'
  rack (1.6.0) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
  /Users/Dan/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/Dan/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/Dan/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
hhh_
  • 310
  • 2
  • 5
  • 17
Dan Mitchell
  • 844
  • 2
  • 15
  • 34
  • 1
    How does your model look like and what gem are you using for file upload? – d4rky Feb 18 '15 at 13:14
  • Not sure if you just didn't include the line, but your model doesn't seem to define `has_attached_file :image` as per [Paperclip's docs](https://github.com/thoughtbot/paperclip#models) – Bart Jedrocha Feb 18 '15 at 13:29
  • @DanMitchell please also include full error with stacktrace, that'll make debugging much easier. For now what Bart says seems like your problem. – d4rky Feb 18 '15 at 16:39
  • @d4rky I have added the stacktrace as Bart's suggestion didn't fix the issue. FYI - I have this exact setup on a Rails 4.0.5 project which works perfectly – Dan Mitchell Feb 18 '15 at 18:38

0 Answers0