Skip to content
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

[ci skip] collection_radio_buttons options example #36760

Merged
merged 2 commits into from
Jul 25, 2019

Conversation

andre-lgf
Copy link
Contributor

Added example of how to access an option attibute passed to builder in case the person wants to add a custom style to a programatically checked value e.g.

Summary

Added example of how to access option passed to the builder (collection_check_boxes).

Other Information

Spent some time figuring out how to add a custom class to a default checked value, since I couldn't find it anywhere in the guide I decided to add it to save others some trouble.

In my case, I was creating a form and wanted to set the first value checked and also add the active class for styling (I'm using bootstrap 4).
Source of code:

<div class='col-md-4 col-sm-12 my-auto mx-auto'>
	<p><%= t('Currency') %></p>
	<div class="btn-group btn-group-toggle" data-toggle="buttons">
		<%= collection_radio_buttons :pricing, :currency_id, @currencies, :id, :symbol, {checked: @currencies.first.id, required: true} do |rb| %>
			<%= rb.label(class: "btn btn-secondary #{'active' if rb.instance_values['input_html_options'][:checked]}") { rb.radio_button + rb.text } %>
		<% end %>
	</div>
</div>

Added example of how to access an option attibute passed to builder in case the person wants to add a custom style to a programatically checked value e.g.
@rails-bot rails-bot bot added the docs label Jul 25, 2019

```html+erb
<%= collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial, {checked: Author.last, required: true} do |rb| %>
<%= rb.label(class: "#{'my-custom-class' if rb.instance_values['input_html_options'][:checked]}") { rb.radio_button + rb.text } %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input_html_options is not public API and people should not be using in they applications. Can you remove this example since it can't be implemented using public APIs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do 'my-custom-class' if rb.value == Author.last to use public API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is way better, thank you. Must be Author.last.id (or any other value used as key) to work though. Thank you for the response, please ignore this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with the correct example.

@rafaelfranca rafaelfranca merged commit 55eba62 into rails:master Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants