Collection_check_boxes lable tag issue.

487 views
Skip to first unread message

Kate

unread,
Mar 17, 2015, 10:17:46 AM3/17/15
to plataformate...@googlegroups.com
I was hoping someone can help me figure out what is going on. I want to customize how the checkboxes are displayed. Mainly to display extra information. I want it to be wrapped by a single label in order to implement something like this: http://www.bootply.com/6YTPUmE8Xh#  What I want is: 

<div class="input check_boxes optional">
  <label class="check_boxes optional" for="article_category_ids"> Category ids</label>
  <span>
    <label for="article_category_ids_4">
      <input id="article_category_ids_4" name="article[category_ids][]" type="checkbox" value="4" />agile</label>
  </span>


But this is what I get, notice the extra label. 

<div class="form-group check_boxes optional order_option_ids">
<label class="check_boxes optional control-label" for="order_option_ids"> Health potion option ids</label>
<span>
<label for="order_option_ids_2">
     <label for="order_option_ids_2">
        <input type="checkbox" value="2" name="order[health_potion_option_ids][]" id="order_option_ids_2">Add more caffeine
     </label>
</label>
</span>

I am doing this, as stated above but I am getting double labels:

<%= f.input :category_ids, :as => :check_boxes do %>
  <%= f.collection_check_boxes :category_ids, Category.order(:name), :id, :name do |b|
    b.label { b.check_box + b.text }
  end %>
<% end %>

According to this: https://github.com/plataformatec/simple_form/issues/452#issuecomment-3954313 it should work but it doesn't in the way shown. 

I'm using: Simple Form 3.1.0 on Rails 4.2. Help is much appreciated. 


THanks

Carlos Antonio da Silva

unread,
Mar 17, 2015, 10:41:41 AM3/17/15
to plataformate...@googlegroups.com
Simple Form is forcing an extra label in case of the "nested" boolean style, due to several reasons of compatibility with bootstrap that I can't remember right now (unfortunately :D).

In any case, you should be able to workaround that by passing `boolean_style: :inline` to your collection_check_boxes call, after all other arguments, and that should get rid of the extra label in this case.

I'll see if I can verify the reasons we're doing that.

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Kate Moczydlowski

unread,
Mar 17, 2015, 12:30:18 PM3/17/15
to plataformate...@googlegroups.com, plataformate...@googlegroups.com
That works for the double labels! Thanks a bunch. Now I have to figure out how to remove those wrapper spans. I have edited the config but can’t seem to find where it forces that span wrapper.

Any suggestions?

Thanks again.



You received this message because you are subscribed to a topic in the Google Groups "SimpleForm" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-simpleform/8g--5ne_Z4k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-simp...@googlegroups.com.

Carlos Antonio da Silva

unread,
Mar 17, 2015, 12:31:35 PM3/17/15
to plataformate...@googlegroups.com
If you set item_wrapper_tag to nil or false I think it should work, you can try passing it as an option too for collection_check_boxes as well.

Kate Moczydlowski

unread,
Mar 17, 2015, 12:37:09 PM3/17/15
to plataformate...@googlegroups.com, plataformate...@googlegroups.com
Great! It works inline as an option. Oddly enough, I did try setting it in the config file and there was no change. Thanks again. 

As a followup. There are a ton of options available for most the tags. Could you recommend a good place for references of the options available for a tag. I’ve been using a mix of rails docs, config browsing, and plain guessing. Wondering if there was a good reference out there.

Thanks again.



On Tue, Mar 17, 2015 at 10:41 AM, Carlos Antonio da Silva <carlosanto...@gmail.com> wrote:

You received this message because you are subscribed to a topic in the Google Groups "SimpleForm" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-simpleform/8g--5ne_Z4k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-simp...@googlegroups.com.

Carlos Antonio da Silva

unread,
Mar 17, 2015, 12:48:38 PM3/17/15
to plataformate...@googlegroups.com
I think most of the options like item_wrapper_tag and boolean_style are simple form only, they are not provided by Rails, only from our wrappers.

Probably the best information you will find about these available options are in the specific method docs: https://github.com/plataformatec/simple_form/blob/v3.1.0/lib/simple_form/form_builder.rb#L369-L421.

You can check for example the input helper in the same file, which contains more docs with examples and available options. And please, if you think anything can be improved, let us know, or feel free to send a pull request :).

Thanks!

Kate Moczydlowski

unread,
Mar 17, 2015, 1:58:29 PM3/17/15
to plataformate...@googlegroups.com, plataformate...@googlegroups.com
Hey there, not done yet! So I got the output that I wanted. I did it like so:

Kate Moczydlowski

unread,
Mar 17, 2015, 2:01:52 PM3/17/15
to plataformate...@googlegroups.com
Sorry about that. Accidentally pressed enter too soon. Here is what I ended up doing:

<%= f.input :option_ids, :as => :check_boxes, wrapper_html: { class: 'btn-group', 'data-toggle' => "buttons"} do %>
          <%= f.collection_check_boxes :option_ids, HealthPotionOption.order(:price),
                                       :id, :name, boolean_style: :inline, item_wrapper_tag: nil do |b|  %>
              <%= b.label(class: "btn btn-primary", html: "was") { "<span class='option_name'> #{b.object.name} </span>".html_safe +
                                                                    "<span class='option_description'> #{b.object.description} </span>".html_safe +
                                                                    "<span class='option_price'> #{b.object.price} </span>".html_safe}
                  %>
            <% end %>
        <% end %>

Is this a horrid way of doing it? Because it is the only way I could figure that I can have all the wrapped in the label for each item. Let me know!

Carlos Antonio da Silva

unread,
Mar 17, 2015, 2:30:45 PM3/17/15
to plataformate...@googlegroups.com
I have to say that the concatenation does not look pretty, moreover, it might be introducing a xss hole if you allow the user to update the name/description/price attributes, since you're html_safing everything. Maybe you could use content_tag for those? It seems better I think.

Also, it seems the final output does not generate checkboxes at all, just a series of buttons, is that correct?

Kate Moczydlowski

unread,
Mar 18, 2015, 12:10:55 AM3/18/15
to plataformate...@googlegroups.com
I blanked and thought for some reason I was bound to one line. Content_tag works great. Thanks a lot for your help.

Carlos Antonio da Silva

unread,
Mar 18, 2015, 6:27:42 AM3/18/15
to plataformate...@googlegroups.com
Awesome, glad I could help :).
Reply all
Reply to author
Forward
0 new messages