Button Labels
Posted by Gregg Kellogg Tue, 24 Oct 2006 02:22:00 GMT
I checked in the button-labels plugin. I was frustrated when trying to create a form with multiple radio-buttons and no way to tell them apart. Basically, if the :label option is passed to radio_button, it calls content_tag() instead of tag() and uses the label as the content.
radio_button("post", "title", "Goodbye World", :label => "Goodbye World")
<input id="post_title_goodbye_world"
name="post[title]"
type="radio"
value="Goodbye World">
<label for="post_title_goodbye_world>
Goodby World
</label>
</input>
The plugin is managed on ruby_forge. Rdoc is available.
Entered some time ago as a bug #4627.
Gregg Kellogg

Updated package to support labels for Check Boxes as well as Radio Buttons.
Beautiful plugin. thanks! one question: why did you not consider doing(labelling) similarly for input/select fields?
It would be a simple extension to add this to other methods, such as input and select. However, the primary motivation for doing this for radio buttons and checklists is that they are are implemented using the tag() rather than content_tag() method (my change makes them content_tag if a label is specified). Given that select is based on content_tag, a the label can simply be added as part of the content. The input tag, however, is based on tag, and so could benefit from supporting a :label option. I just haven’t found it necessary when creating forms.
Please consider the following patch, which brings the ID attribute in line with Rails defaults.
Thanks for the suggestion. I’ve implemented your changes, and made a change to the 2.3 calling sequence to InstanceTag.
Where can I find your changes for Rails 2.3?
It seems version 1.0.5 still outputs the hidden field after the input field…
Pretty much just implemented your patch. I just created a new 2.3 app, installed the pluging and created a simple form as follows:
I don’t see any hidden field in the output:
Download as follows; script/plugin install svn://rubyforge.org/var/svn/button-labels/trunk
Ah, the hidden field comes into play with checkboxes, not with radio buttons.
Before Rails 2.3, a hidden field with the unchecked value was included before the checkbox field.
Starting with Rails 2.3, and most likely caused by the switch to Rack, the hidden field with the unchecked value should occur after the checkbox field.
Thus, please consider this patch, which brings the code in line with Rails 2.3:
Index: vendor/plugins/button-labels/button-labels/lib/button_labels.rb =================================================================== --- vendor/plugins/button-labels/button-labels/lib/button_labels.rb (revision 1963) +++ vendor/plugins/button-labels/button-labels/lib/button_labels.rb (working copy) @@ -118,11 +118,13 @@ end options["checked"] = "checked" if checked add_default_name_and_id(options) - if options.has_key?("label") + hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value) + checkbox = if options.has_key?("label") content_tag("input", content_tag(:label, options.delete("label"), :for => options["id"]), options) else tag("input", options) - end << tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value) + end + hidden + checkbox end end endI pushed up the change to place the hidden field before the input. Version 1.0.6.