Multiple upload with Paperclip in ror

让人想犯罪 __ 提交于 2019-11-30 16:27:08
tmaximini

Yes you can do it with paperclip. The way I do it is with nested resources and the nested_form gem.

For example you building has_many :photos and photo belongs_to :building

Then in your /views/buildings/_form.html.erb you would write something like this:

<%= nested_form_for @building, :html => { :multipart => true } do |f| %>
  <%# all your building fields .... %>

  <%= f.fields_for :photos do |photo| %>  
    <% if photo.object.new_record? %>
      <%= photo.file_field(:image) %>
    <% else %> 
      <%= image_tag(photo.url(:thumb)) %>
      <%= photo.hidden_field :_destroy %>
      <%= photo.link_to_remove "X" %>
    <% end %>
  <% end %>

  <p><%= f.link_to_add "Add photo", :photos %></p>

  <%= f.submit %>
<% end %>

You will have to set accepts_nested_attributes_for :photos, :allow_destroy => true inside your building.rb model and make sure your routes.rb also include the nesting:

resources :buildings do 
  resources :photos
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!