Skip to content

Commit

Permalink
Allow using different value and label for radio control options
Browse files Browse the repository at this point in the history
  • Loading branch information
ptzn committed Jan 22, 2010
1 parent c15b5ab commit 0bb67db
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/semantic_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ def image_submit_button(image_file, options={})
#
# ex:
# f.radio_buttons :gender, :choices => [ :male, :female ]
# also you can provide array of arrays to use different option value and caption
# f.radio_buttons :gender, :choices => [ ['Male', 1], ['Female', 2] ]
#
def radio_buttons(attribute, options)
caption = (options.delete(:label) || attribute.to_s.titleize).gsub(' ', ' ') + ":"
html = @template.content_tag(:dt, @template.content_tag(:span, caption))
html << @template.content_tag(:dd) do
returning choices_html = String.new do
options[:choices].each do |choice|
choices_html << radio_button(attribute, choice.to_s, :class => 'radio') +
@template.content_tag("label" , "#{choice.to_s.titleize}", :for => "#{object_name}_#{attribute}_#{choice.to_s}" )
label, value = choice.is_a?(Array) ? [choice.first, choice.last.to_s] : [choice.to_s.titleize, choice.to_s]
choices_html << radio_button(attribute, value, :class => 'radio') +
@template.content_tag("label" , label, :for => "#{object_name}_#{attribute}_#{value}" )
end
end
end
Expand Down

0 comments on commit 0bb67db

Please sign in to comment.