New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Styling the labels for a check box collection #452
Comments
Any collection helper, including Rails ones (ie collection_select) handles the third option in the collection as html attributes, unfortunately. I think the best you can do is to generate the check boxes + labels by hand, the old way. This has been a pain in the ass for some time, I think it's time to solve that somehow in Rails and allow SimpleForm to help on that. I'll leave the issue open as a reminder for me to find a workaround and improve this. Btw, please use the mailing list for questions ok. I believe there might be some answers there that could help you as well. |
So, I have scratched something to help handling such cases in SimpleForm, as you can see in e9bb328. SimpleForm collection helpers are able to receive a block in the new 2.0.rc version, which means you should be able to do something like that with current master: f.input :font_ids, :as => :check_boxes do
f.collection_check_boxes :font_ids, Font.all, :id, :name do |b|
b.check_box + b.label(:class => b.object.class_name)
end
end
Update: Please give it a try and let us know about any issue. Thanks. |
Thanks man but I get this: undefined method `check_box' for "font_ids_10":String Joshua Lippiner On Feb 13, 2012, at 9:22 PM, Carlos Antonio da Silva wrote:
|
Are you sure b is the right builder inside I've just created an example app with article + categories as example, using master, and it worked. Here is the usage: <%= 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 %> And the ouput <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>
<span>
<label for="article_category_ids_2">
<input id="article_category_ids_2" name="article[category_ids][]" type="checkbox" value="2" />design</label>
</span>
<span>
<label for="article_category_ids_1">
<input id="article_category_ids_1" name="article[category_ids][]" type="checkbox" value="1" />development</label>
</span>
<span>
<label for="article_category_ids_5">
<input id="article_category_ids_5" name="article[category_ids][]" type="checkbox" value="5" />events</label>
</span>
<span>
<label for="article_category_ids_3">
<input id="article_category_ids_3" name="article[category_ids][]" type="checkbox" value="3" />project management</label>
</span>
<input name="article[category_ids][]" type="hidden" value="" />
</div> |
Well something is up and most likely it's the version im on. I'm running... gem 'simple_form', '~> 2.0rc' and when inspect "b" I get a string literal, like this - "font_ids_21" and not an object, which I believe I should be seeing. Any thoughts? Also, Im running HAML but that shouldn't make a difference. J On Feb 13, 2012, at 10:11 PM, Carlos Antonio da Silva wrote:
|
Ah, you have to bundle from master to test it, I did that change yesterday. I think I didn't mention that, sorry :D |
Ahh - not sure I've done that before :) So I cant use bundler for this? Best, J Joshua Lippiner On Feb 14, 2012, at 4:26 AM, Carlos Antonio da Silva wrote:
|
Yeah, you just need to give the
Then run |
ahh - ill give that a try in a bit. I see what you are saying now :) Joshua Lippiner On Feb 14, 2012, at 9:50 AM, Carlos Antonio da Silva wrote:
|
It works! Thanks! Joshua Lippiner On Feb 13, 2012, at 9:22 PM, Carlos Antonio da Silva wrote:
|
Awesome, thanks. |
@carlosantoniodasilva sorry to revive an old thread, but is there an easier way to do this now with the new wrapper syntax? I'll add the solution to the wiki if you'd like. |
No. The only way right now is using what is described here #452 (comment) |
Anyone know why I get this output: <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 %> using simple_form (3.1.0) |
@digitalecho please try sending your question to the mailing list, there are more people there that might help. |
same issue as @digitalecho here, did you figure it out? |
I'm having the same issue, labels get doubled trying to manually render labels |
@aaronmcadam could you provide a sample app showing the issue? Please feel free to open a new issue with the app then, thanks. |
Hi @carlosantoniodasilva, thanks for replying. I've created a new issue. It looks like the issue happens when using the default configuration. Is there a way to ignore it? |
I have a simple HABTM relationship between categories and fonts. A category can have many fonts. Anyway, in my form I want to display a list of all the fonts with a checkbox next to each of them so I can select the fonts for a category.
The catch is that I want to add the font class to the LABEL for each option so that it is applied. In this case, the class is an attribute called class_name inside of the font model.
I have this:
and have tried lots of things, including:
without luck. How can I accomplish this?
The text was updated successfully, but these errors were encountered: