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

Posted in  | 9 comments | no trackbacks

Comments

  1. Avatar Gregg Kellogg said 121 days later:

    Updated package to support labels for Check Boxes as well as Radio Buttons.

  2. Avatar Jatinder Singh said 165 days later:

    Beautiful plugin. thanks! one question: why did you not consider doing(labelling) similarly for input/select fields?

  3. Avatar Gregg Kellogg said 184 days later:

    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.

  4. Avatar Christian Rishøj said 850 days later:

    Please consider the following patch, which brings the ID attribute in line with Rails defaults.

    
    Index:/button-labels/lib/button_labels.rb
    ===================================================================
    ---/button-labels/lib/button_labels.rb (revision 1065)
    +++/button-labels/lib/button_labels.rb (revision 1507)
    @@ -54,8 +54,10 @@
           #     <%=  radio_button_tag "people", "david", false, "label" => "david" %>
           def radio_button_tag_with_label(name, value, checked = false, options = {})
    -        html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
    +        pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase
    +        pretty_name = name.to_s.gsub(/\[/, "_").gsub(/\]/, "")        
    +        html_options = { "type" => "radio", "name" => name, "id" => "#{pretty_name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys)
             html_options["checked"] = "checked" if checked
             if (options.has_key?(:label))
    -          content_tag :input, content_tag(:label, html_options.delete("label"), :for => name), html_options
    +          content_tag :input, content_tag(:label, html_options.delete("label"), :for => "#{pretty_name}_#{pretty_tag_value}"), html_options
            else
               tag :input, html_options
    
    
  5. Avatar Gregg Kellogg said 867 days later:

    Thanks for the suggestion. I’ve implemented your changes, and made a change to the 2.3 calling sequence to InstanceTag.

  6. Avatar Christian Rishøj said 917 days later:

    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…

  7. Avatar Gregg Kellogg said 919 days later:

    Pretty much just implemented your patch. I just created a new 2.3 app, installed the pluging and created a simple form as follows:

    
    <% form_for(@label_test) do |f| %>
      <%= f.error_messages %>
    
      <p>
        <%= f.radio_button("title", "Goodbye World", :label => "Goodbye World") %>
        <%= f.submit 'Create' %>
      </p>
    <% end %>
    
    

    I don’t see any hidden field in the output:

    
      <p>
        <input id="label_test_title_goodbye_world" 
            name="label_test[title]" type="radio" value="Goodbye World">
            <label for="label_test_title_goodbye_world">
                Goodbye World
            </label>
        </input>
        <input id="label_test_submit" name="commit" type="submit" value="Create" />
      </p>
    
    

    Download as follows; script/plugin install svn://rubyforge.org/var/svn/button-labels/trunk

  8. Avatar Christian Rishøj said 920 days later:

    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
       end
    
  9. Avatar Gregg Kellogg said 927 days later:

    I pushed up the change to place the hidden field before the input. Version 1.0.6.

Trackbacks

Use the following link to trackback from your own site:
http://kellogg-assoc.com/articles/trackback/5

(leave url/email »)

   Comment Markup Help Preview comment