Created
February 4, 2012 03:12
-
-
Save underwhelmed/1734871 to your computer and use it in GitHub Desktop.
Multiple File Upload In Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Album < ActiveRecord::Base | |
has_many :photos, :dependent => :destroy | |
accepts_nested_attributes_for :photos, :allow_destroy => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= javascript_include_tag :defaults, 'jquery-1.3.2.min' %> | |
<script> | |
jQuery.noConflict(); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function remove_field(element, item) { | |
element.up(item).remove(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :albums do | |
resources :photos | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://railsforum.com/viewtopic.php?id=35044