Rails 3 -NoMethodError (undefined method `original_filename
I'm using Rails 3, Uploadify, to send images to S3.
Right now all the images being upload have the MIME: application/octet-stream
I'd like to fix that but I'm getting the following error:
NoMethodError (undefined method `original_filename' for #<ActiveSupport::HashWithIndifferentAccess:0x107c81998>):
app/models/photo.rb:29:in `upload_file='
app/controllers/photos_controller.rb:15:in `upload'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
I think this is because all the tutori开发者_StackOverflow中文版als out there aren't Rails 3 friendly. Anyone have any ideas? Here's the code:
# Controller
def create
@photo = Photo.new(:upload_file => params[:photo][:image])
...
end
# Model
class Photo < ActiveRecord::Base
require 'mime/types'
...
def upload_file=(data)
data.content_type = MIME::Types.type_for(data.original_filename).to_s
self.image = data
end
end
I'm not familiar with Uploadify, but it seems to be just a javascript generator...
You're passing a params value in as 'data' for #upload_file= . Then you're calling a method (#original_filename) on params[:photo][:image]. Rails is saying that params[:photo][:image] doesn't have such a method.
Is there some kind of File class in 'mime/types'? Should you be creating that File object first?
file = File.new(params[:photo][:image])
and then change that files attribute:
file.content_type = ...
EDIT:
Are you using the paperclip gem? The tutorial that you are using is using paperclip. So in "@asset.file_content_type = MIME::Types.type_for(@asset.original_filename).to_s", I think @asset is an instance of paperclip's File class which does have a #original_filename method. However, I don't see a #file_content_type=() method in the paperclip docs.
精彩评论