rename image before saving via paperclip - rails 2.3.5 -
i using paperclip allow user upload images in rails application.
i want rename images uploaded appending current time file names before saving them in database.
here model. column 'data_file_name' used store image name in 'images' table.
class image < activerecord::base set_table_name "images" set_primary_key "id" before_create : :rename_image_file def rename_image_file extension = file.extname(data_file_name).downcase self.data_file_name.instance_write(:data_file_name, "#{time.now.to_i.to_s}#{extension}") end belongs_to :book,:foreign_key => :book_id has_attached_file :data, :path => ":rails_root/public/book_images/:book_id/:style/:basename.:extension", :url => "http://www.test.org/book_test_editors/book_images/:book_id/:style/:basename.:extension", :styles => { :thumbnails => ["150x172#",:jpg], :large => ["100%", :jpg] } end when try upload image, error message below:
nomethoderror in bookscontroller#update undefined method `instance_write' "dsc02017.jpg":string any help?
note using rails 2.3.5
you can adjust path change file name e.g.
has_attached_file :data, :path => ":rails_root/public/book_images/:book_id/:style/:basename.#{time.now.to_i}.:extension",
Comments
Post a Comment