Skip to content

Instantly share code, notes, and snippets.

@underwhelmed
Created February 4, 2012 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save underwhelmed/1734871 to your computer and use it in GitHub Desktop.
Save underwhelmed/1734871 to your computer and use it in GitHub Desktop.
Multiple File Upload In Rails
<% form_for(@album, :html => { :multipart => true }) do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<h2>Photo(s)</h2>
<div id="photos">
<% if @album.new_record? %>
<%= render :partial => 'photo', :locals => { :form => f, :photo => @album.photos.build } %>
<% else %>
<% @album.photos.each do |photo| %>
<%= image_tag photo.upload.url(:small) %>
<% end %>
<% end %>
</div>
<%= add_object_link("New Photo", f, @album.photos.build, "photo", "#photos") %>
<p><%= f.submit %></p>
<% end %>
<div class="photo">
<%= form.fields_for :photos, photo, :child_index => (photo.new_record? ? "index_to_replace_with_js" : nil) do |photo_form| %>
<p>
<%= photo_form.label :title %>
<%= photo_form.text_field :title %>
<%= photo_form.file_field :upload %>
<%= link_to_function "delete", "remove_field($(this), ('.photo'))" %>
</p>
<% end %>
</div>
class Album < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
end
module AlbumsHelper
def add_object_link(name, form, object, partial, where)
html = render(:partial => partial, :locals => { :form => form}, :object => object)
link_to_function name, %{
var new_object_id = new Date().getTime() ;
var html = jQuery(#{js html}.replace(/index_to_replace_with_js/g, new_object_id)).hide();
html.appendTo(jQuery("#{where}")).slideDown('slow');
}
end
def js(data)
if data.respond_to? :to_json
data.to_json
else
data.inspect.to_json
end
end
end
<%= javascript_include_tag :defaults, 'jquery-1.3.2.min' %>
<script>
jQuery.noConflict();
</script>
function remove_field(element, item) {
element.up(item).remove();
}
<h1>Listing albums</h1>
<table>
<tr>
<th>Name</th>
<th>Photo Title</th>
<th>Photos</th>
</tr>
<% @albums.each do |album| %>
<tr>
<td><%=h album.name %></td>
<% album.photos.each do |photo| %>
<td><%=h photo.title %></td>
<td><%= image_tag photo.upload.url(:thumb) %></td>
<% end %>
<td><%= link_to 'Show', album %></td>
<td><%= link_to 'Edit', edit_album_path(album) %></td>
<td><%= link_to 'Destroy', album, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New album', new_album_path %>
class Photo < ActiveRecord::Base
belongs_to :album
has_attached_file :upload,
:url => "/images/:id/:style/:basename.:extension",
:path => ":rails_root/public/images/:id/:style/:basename.:extension",
:styles => {
:thumb => "75x75>",
:small => "200x200>"
}
#add in any validations you may want
end
resources :albums do
resources :photos
end
<p>
<b>Name:</b>
<%=h @album.name %><br />
<% @album.photos.each do |photo| %>
<h3><%=h photo.title %></h3>
<%= image_tag photo.upload.url(:small) %>
<% end %>
</p>
<%= link_to 'Edit', edit_album_path(@album) %> |
<%= link_to 'Back', albums_path %>
@underwhelmed
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment