Gem paperclip 怎样实现给一个商品对象上传多张图片?

gechentuo · 2013年12月20日 · 最后由 gechentuo 回复于 2013年12月20日 · 2698 次阅读

今天遇到了一个问题,我使用了 paperclip 这个 gem 来实现图片的上传,在我的系统中,有 Item (商品这个类)

Modle

class Item < ActiveRecord::Base
 has_attached_file :image, :styles => { :medium => "300*300>" , : thumb => "100*100>" }

View

<%= form_for @item, :url => items_path, :html => { :multipart => true } do |f| %> 
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="field">
      <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这样每个 Item 对象只能存一个图片,怎样给一个 item 上传多张图片?

class Item < ActiveRecord::Base
  has_many :photos
end
class Photo < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "300*300>" , : thumb => "100*100>" }
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号