Ruby on rails-paperclip不保存到数据库

时间:2014-01-26 00:56:22

标签: ruby-on-rails ruby paperclip

我是铁轨世界的新手。我要做的是为用户添加添加图片的功能。我做了一些研究,我找到了一个名为“paperclip”的宝石,但是在安装之后。每次我尝试上传图片时,都会“选择无文件”。

这是我的模特

attr_accessible :description, :image
 validates :description, presence: true
 validates :user_id, presence: true
 belongs_to :user 
 has_attached_file :image , :styles => { :small => "150x150>" }

 validates_attachment :image , :presence => true,
 :content_type => { :content_type => ["image/jpeg", "image/jpg","image/png","image/gif"] },
 :size => { :in => 0..10.kilobytes }

这是我的观点

<div class="form-inputs">
 <%= f.input :image , label: "upload a picture !! " %>
<%= f.input :description , as: :text ,input_html: { rows:"3"} %>


1 个答案:

答案 0 :(得分:1)

一步一步

宝石

gem "paperclip", "~> 3.0"

终端

$ bundle install
$ rails generate paperclip user image
$ rake db:migrate

用户模型

class User < ActiveRecord::Base
  attr_accessible:image, :description

  validates :description, presence: true
  validates :user_id, presence: true

  has_attached_file :image, styles: { medium: "320x240>"}

  validates_attachment :image, presence: true,
                            content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                            size: { less_than: 5.megabytes } # you wanna change that
  belongs_to :user

end

您的观点还可以,请不要忘记rake db:migraterestart your Server