From 1a0e9f5125e62dd441f2901270db9a1f2c9c96b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Galatti=20Faria?= Date: Wed, 24 Jul 2019 22:50:11 -0300 Subject: collection_radio_buttons options example 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. --- guides/source/action_view_overview.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/guides/source/action_view_overview.md b/guides/source/action_view_overview.md index a1b69edd22..c0b53428c7 100644 --- a/guides/source/action_view_overview.md +++ b/guides/source/action_view_overview.md @@ -1025,6 +1025,34 @@ If `@article.author_id` is 1, this would return: ``` +Recovering some option passed (e.g. programatically checking an object from collection): + +```ruby +collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial, {checked: Author.last}) +``` + +In this case, the last object from the collection will be checked: + +```html + + + + + + +``` + +To access the passed options programatically (e.g. adding a custom class if checked): + +**Sample html.erb** + +```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 } %> +<% end %> +``` + + #### collection_check_boxes Returns `check_box` tags for the collection of existing return values of `method` for `object`'s class. -- cgit v1.2.3 From 1816348f7e4ee46bb82b96a6f1469448fd9fc204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Galatti=20Faria?= Date: Thu, 25 Jul 2019 11:34:10 -0300 Subject: Update action_view_overview.md --- guides/source/action_view_overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/action_view_overview.md b/guides/source/action_view_overview.md index c0b53428c7..dda3ae0863 100644 --- a/guides/source/action_view_overview.md +++ b/guides/source/action_view_overview.md @@ -1048,7 +1048,7 @@ To access the passed options programatically (e.g. adding a custom class if chec ```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 } %> + <%= rb.label(class: "#{'my-custom-class' if rb.value == Author.last.id}") { rb.radio_button + rb.text } %> <% end %> ``` -- cgit v1.2.3