GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add block to `collection_check_boxes` and `collection_radio_buttons`. · Issue #477 · bootstrap-ruby/bootstrap_form

Add block to collection_check_boxes and collection_radio_buttons. #477

New issue
Copy link
New issue
Copy link
Open
Open
Add block to collection_check_boxes and collection_radio_buttons.#477
Copy link
Assignees
lcreid
Labels
new featureyou can help!

Description

@lcreid
lcreid
opened on Jul 15, 2018
Issue body actions

The Rails collection_check_boxes and collection_radio_button methods can take a block. If a block is provided, the helper yields a special builder to the block. The idea is that the programmer can format the check box or radio button more precisely in the block, if the standard format produced by the Rails helpers doesn't give the desired results.

Our helpers do not take a block. We can speculate that since our helpers format the check boxes and radio buttons in a consistent and pleasing way, there's less need for the block. There may also have been a technical reason that made yielding to a block difficult. I haven't investigated enough to have an idea of that.

However, there still may arise situations in which it would be convenient to have the ability to output the controls in a block. On example would be when you want to show all the check boxes or radio buttons, but some of them need to be disabled. Another example might be to highlight certain "dangerous" check boxes or radio buttons with the "danger" colour.

Another argument in favour of providing this is simply to make our helpers more closely mimic the behaviour of the Rails helpers. I bet there are a number of cases of people wasting an hour or two trying to get a block to work with our helpers before they realized that our helper doesn't work that way.

One alternative is for programmers to code the loop themselves. This is probably not a large burden, although they would have to remember to add the div.form-group and the error handling in order to be 100 percent compatible with our helpers.

Would we consider a pull request for this functionality if someone submitted one?

Reacted by Akshat Kedia, Jose Farias, stephan.com, Dima Pashutskii, Myeonghwan Cho and Carlos Ramirez III

Activity

  1. lcreid
    added
    new feature
    on Jul 15, 2018
  2. lcreid commented on Jul 23, 2018

    @lcreid
    lcreid
    on Jul 23, 2018
    ContributorAuthor
    More actions

    Here's a specific example I'm having trouble implementing: I have a has_many :team_members, through: on the Subject model. I want to provide a simple user interface with a check boxes for each person on staff. Checking the box makes them a member of the team. However, I want to prevent the user from removing themself from the team, so I don't want to show the check box for the current user.

    In ordinary Rails I could do this:

    <%= form_for @subject do |f| %>
      <%= f.collection_check_boxes(:team_member_ids, Person.all, :id, :name) do |builder| %>
        <% unless current_user == builder.object %>
          <%= builder.check_box %><%= builder.name %>
        <% end %>
      <p><%= f.submit %></p>
    <% end %>
    

    I can't seem to find a similarly elegant way to do this with bootstrap_form's collection_check_box. @mattbrictson or anyone else, do you have any ideas for this? Am I missing something?

  3. lcreid
    added
    you can help!
    on Feb 8, 2019
  4. stephancom commented on May 23, 2019

    @stephancom
    stephancom
    on May 23, 2019
    More actions

    Another thought I'm having is perhaps in addition to the value_method and text_method parameters, add an options_method parameter. This could be a proc, which gets called for each item, receives the object as a parameter, and generates additional options for the underlying check_box.

    In my case, I want the checkboxes to toggle visibility of a div elsewhere on the page, so I might do something like:

    = f.collection_check_boxes :toggleable_things, Toggleable.all, :id, :name, ->(t) { { data: { toggle: dom_id(t) } } }
    

    and thus each checkbox would have an attribute like data-toggle="toggleable_123"

    In @lcreid 's example right now, I would just replace Person.all with something like Person.where.not(id: current_user.id) (why include something in the collection just to skip it?)- however, maybe you want that shown but disabled, so you might do:

     = f.collection_check_boxes(:team_member_ids, Person.all, :id, :name, ->(p) { { disabled: p == current_user } })
    

    This seems like it should be a bit easier to manage and fairly elegant. Another parameter could be permitted for label_options. Each could also be permitted as a symbol which calls that method on the underlying object.

  5. swrobel
    added a commit that references this issue on Aug 8, 2019

    Link to & provide workaround for bootstrap-ruby#477

    dcd9745
  6. lcreid
    added a commit that references this issue on Aug 11, 2019

    Link to & provide workaround for #477 (#542)

    1f28ab0
  7. lcreid
    self-assigned this
    on Aug 29, 2019
  8. lapser commented on Nov 27, 2023

    @lapser
    lapser
    on Nov 27, 2023
    More actions

    One alternative is for programmers to code the loop themselves. This is probably not a large burden, although they would have to remember to add the div.form-group and the error handling in order to be 100 percent compatible with our helpers.

    @lcreid can you provide an example to be 100 percent compatible with your helpers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @lcreid
    lcreid

Labels

new featureyou can help!

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.