Skip to content

Instantly share code, notes, and snippets.

@billsaysthis
Created September 21, 2010 20:39
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 billsaysthis/590486 to your computer and use it in GitHub Desktop.
Save billsaysthis/590486 to your computer and use it in GitHub Desktop.
# Adapted from http://github.com/marcomd/Logbook/blob/e3537af3170442b0a2cf93a5060c645643b88446/app/helpers/application_helper.rb
# usage example
# = manage_to_many_checkbox(@projects, @user, "project", 3)
# add to application_helper.rb
def manage_to_many_checkbox(collection, object, str_object_many, cols=1)
html = ""
i=0
cb_style = cols == 1 ? '' : 'float:left;width: #{100/cols}%;'
collection.each do |item|
id_html = "#{str_object_many}_#{item.id.to_s}"
fld_cnt = content_tag(:span,
check_box_tag("#{str_object_many}_ids[]",
item.id,
object.send("#{str_object_many}s").include?(item),
{:id => id_html}) +
label_tag(id_html, item.name),
:style => cb_style)
if i % cols == 0 && cols > 1
html << content_tag(:div, fld_cnt, :style => 'display: block')
elsif i % cols == 0 && cols == 1
html << fld_cnt
html << tag(:br)
else
html << fld_cnt
end
i+=1
end
html << content_tag(:div, '', :class => 'clear')
raw html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment