Subhashmohan's Blog

Active Scaffold : Add checkbox array for a particular field

Posted on: November 18, 2010

Suppose you have a field in your table, which accepts array of values through multiple checkboxes, then here is a way to show these checkboxes on your active scaffold create/update form.

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name,  :gender, :checklist ]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [[‘Male’, ‘1’], [‘Female’,’2′]]
end
end

Assume ‘checklist’ is the column which accepts an array of values, then add the below chunk of code to your users helper

#Users helper

module UsersHelper
def checklist_form_column(record, options)
html = ‘ ‘
(1..5).each do |val|
html << check_box_tag(“record[checklist][]”, val, :id => “check_#{val}”)
html << “<label for=’check_#{val}’>”
html << “lable_#{val}”
html << “</label>”
html << “<br/>”
end
html
end
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats

  • 3,807 hits
November 2010
M T W T F S S
« Mar   Mar »
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

Pages

%d bloggers like this: