我正在使用Paperclip上传图片,但由于某些原因,paperclip不会在我的rails应用程序中保存图像。
这是我的控制器
def create
@product = Product.new(params[:product])
if @product.save
# flash[:notice] => "Product successfully added"
redirect_to product_path @product.id
else
#flash[:notice] => "Something went wrong"
render new_product_path
end
end
和我的模特
class Product < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
#validates :name, :presence => true
has_attached_file :photo
# has_one :category
attr_accessible :name, :description, :from, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
end
和查看
<%= form_for(@product, :html => { :multipart => true }) do |f| %>
<p> <%= f.label :name %><br />
<%= f.text_field :name %></p>
<p> <%= f.label :description %><br />
<%= f.text_area :description, :rows => 4%></p>
<p> <%= f.label :price %><br />
<%= f.text_field :from %></p>
<p><%= f.label :photo%><br />
<%= f.file_field :photo%></p>
迁移
class AddAttachmentPhotoToProduct < ActiveRecord::Migration
def self.up
add_column :products, :photo_file_name, :string
add_column :products, :photo_content_type, :string
add_column :products, :photo_file_size, :integer
add_column :products, :photo_updated_at, :datetime
end
def self.down
remove_column :products, :photo_file_name
remove_column :products, :photo_content_type
remove_column :products, :photo_file_size
remove_column :products, :photo_updated_at
end
end
当我试图验证照片的存在时;验证失败。任何帮助,将不胜感激。此外,我想重新调整大小/缩小尺寸并仅保存调整大小的照片,因为我在数据库中的空间有限。