Skip to content
Browse files

Allow using different value and label for radio control options

  • Loading branch information...
1 parent c15b5ab commit 0bb67dbda3d14afc0c87c7090479f61a936d6bb5 @ptzn ptzn committed Jan 22, 2010
Showing with 5 additions and 2 deletions.
  1. +5 −2 lib/semantic_form_builder.rb
View
7 lib/semantic_form_builder.rb
@@ -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

0 comments on commit 0bb67db

Please sign in to comment.
Something went wrong with that request. Please try again.