问题
At the moment I try to replace attachment_fu fileuploading plugin with paperclip.
But there are some errors while uploading:
My controller looks like this:
def create
add_breadcrumb 'breadcrumb.upload_file', {:action => :new}
puts "BLUBBBBBBBBBBBBBBBBBBBBBBBBBBB"
puts params[:upload]
@upload = Upload.create(params[:upload])
@upload.lecturer_id = current_user.id
if @upload.save
flash[:notice] = I18n.t("flash.saved")
redirect_to :action => :index
else
render :action => :new
end
end
and my model like this:
puts has_attached_file :image, :default_url => :file_system
validates_attachment_content_type :image, :content_type => [:image, 'audio/mpeg', 'application/mp3', 'application/octet-stream']
While uploading I got this error:
ActiveRecord::UnknownAttributeError in UploadsController#create
unknown attribute: uploaded_data
app/controllers/uploads_controller.rb:24:in `create'
Update:
{"name"=>"TEST", "uploaded_data"=>#<ActionDispatch::Http::UploadedFile:0x00000005518a38 @original_filename="2014-09-26 18.14.22.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[uploaded_data]\"; filename=\"2014-09-26 18.14.22.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20150415-12671-190wpi5>>}
Update: thats not that what I wanted. The migration generetad some new columns in database, how can I return to the previus version? is there a demigrate coammand?
回答1:
To revert the migration from Paperclip you can use either:
rake db:migrate:down VERSION=_current_migration_number
or
rake db:rollback
Until your previous state. Then you can create a new migration like this:
class ChangeUploadToPaperclip < ActiveRecord::Migration
change do
rename_column :uploads, :content_type, :upload_content_type
rename_column :uploads, :filename, :upload_filename
rename_column :uploads, :size, :upload_file_size
add_column :uploads, :upload_updated_at, :datetime
# Warning: the following will remove your old data, irreversible.
remove_column :uploads, :width
remove_column :uploads, :height
remove_column :uploads, :thumbnail
end
end
But there could be potential errors between your old storage and Paperclips way of storing files.
In this answer I assumed that your model has_attached_file :upload.
If you have another name for the attached file, update every upload and uploads accordingly.
Edit The migration file needs to have a certain name:
20150417125122_change_upload_to_paperclip.rb
(the date-string is auto generated, I copied this from comment below.)
This is because Rails will look for ChangeUploadToPaperclip only if there is underscore between.
If you have changeuploadtopaperclip.rb it would look for Changeuploadtopaperclip.
Running special migration
rake db:migrate:up VERSION=20150417125122
回答2:
your column for image is image from your model structure. But the data in params the image is coming in params[:model_name][uploaded_data].
Your model doesnt contain any column with name uploaded_data that too for a attached_file
In your form_for you should have this
= f.file_field :image
not
= f.file_field :uploaded_data
In this way your params would look like
{"name"=>"TEST", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000005518a38 @original_filename="2014-09-26 18.14.22.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[uploaded_data]\"; filename=\"2014-09-26 18.14.22.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20150415-12671-190wpi5>>}
And it wont give you any error
GO through the gem on github paperclip
You nee to create migration file like this:
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def self.up
add_attachment :model_name, :[image]
end
def self.down
remove_attachment :model_name, :[image]
end
end
Or use the generator:
rails generate paperclip model_name [image]
来源:https://stackoverflow.com/questions/29647970/errors-while-using-paperclip-in-rails-app