2014-01-26 63 views
0

我是一個新的軌道世界。我想要做的是添加能力給用戶添加圖片。我做了一些研究,我發現了一個名爲「回形針」的寶石,但安裝後。每次我嘗試上傳圖片時,都會顯示「沒有選中文件」。紅寶石軌道回形針不保存到數據庫

這是我的模型

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"} %> 


回答

0

分步

寶石

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