Skip to content

Instantly share code, notes, and snippets.

@Preacher
Created May 18, 2011 18:25
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 Preacher/979176 to your computer and use it in GitHub Desktop.
Save Preacher/979176 to your computer and use it in GitHub Desktop.
paginated ability to add photos to articles via habtm association - lacks ability to select photos from multiple galleries
$(document).ready(function() {
// ...
$('.selector_control').live('click', function() {
$(this).parents('.select_galleries').find('.select_photos').slideToggle();
});
$('.all_photos').click(function(){
$(this).closest('.select_galleries').find("INPUT[type='checkbox']").attr('checked', true);
});
$('.no_photos').click(function(){
$(this).closest('.select_galleries').find("INPUT[type='checkbox']").attr('checked', false);
});
});
class ArticlesController < ApplicationController
# ...
def select_photos
@article = Article.find_by_permalink!(params[:id])
@galleries = Gallery.published.order('published_at DESC').page(params[:page]).per(10)
end
def add_photos
@article = Article.find_by_permalink!(params[:id])
params[:article][:photo_ids] ||= []
if @article.update_attributes(params[:article])
redirect_to article_url(@article), :notice => "Successfully added photos to article."
else
render :action => 'select_photos'
end
end
end
<h1>
Add Photos to <%= @article.title %>
</h1>
<% @galleries.each do |gallery| %>
<div class="select_galleries">
<h3 class="selector_control">
<%= gallery.title %> &bull;
<span>Show/Hide</span>
</h3>
<ul class="select_photos" style="display:none;">
<div class="photos_selector">
<span class="all_photos">Select All</span> |
<span class="no_photos">Deselect All</span>
</div>
<%= form_for gallery, :url => add_photos_article_path(@article), :html => {:method => :put} do |f| %>
<%= f.hidden_field(:title, :value => @article.title) %>
<% gallery.photos.each do |photo| %>
<li class="select_photo">
<span>
<%= check_box_tag "article[photo_ids][]", photo.id, @article.photos.include?(photo), :class => 'selectable_photo' %>
<%= label_tag :include %>
</span>
<br />
<%= image_tag photo.image.url(:thumbnail) %>
</li>
<% end %>
<br class="clearfloat" />
<%= f.submit 'Save Photos' %>
<% end %>
</ul>
</div>
<% end %>
<div id="paginator">
<%= paginate @galleries %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment