I am using paperclip to upload one photo for one building. http://www.youtube.com/watch?v=KGmsaXhIdjc i have done it with this way. But know I decide to upload many photos to one building. Can I do that with paperclip or have to change it and use jQuery ? And if I can how ? Ps: if needed I will upload my code. ps: i am thinking to make 2 more columns in database for photo2 and photo 3.. btw i have see all generates assets to do that. the think is that i have make it with different way to upload 1 photo. this means i have to change the all thing?
update 1:
in routes.rb
resources :buildings do
resources :photos
end
in buldings>_form
<%= form_for(@building, :html=>{:multipart => true}) do |f| %>
<% if @building.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@building.errors.count, "error") %> prohibited this building from being saved:</h2>
<ul>
<% @building.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.select :status, Building::STATUS, :prompt=>'Select status of the building' %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 10 %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<!--<%= f.label :photo %><br />
<%= f.file_field :photo %> -->
<%= 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>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
in model>building
attr_accessible :description, :price, :status, :title, :photo
accepts_nested_attributes_for :photo, :allow_destroy => true
has_attached_file :photo ,
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
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
来源:https://stackoverflow.com/questions/14880100/multiple-upload-with-paperclip-in-ror