diff options
author | Rafael França <rafael@franca.dev> | 2019-07-25 10:49:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-25 10:49:40 -0400 |
commit | 55eba62a1074a4c1e70a9b96b78b3dbf812f5e9a (patch) | |
tree | 5ac80a14751ab9c5bf408234df169973a3eaf6e8 | |
parent | 4ee88df6a01eadd8abbb07f2c17821a9512f51d0 (diff) | |
parent | 1816348f7e4ee46bb82b96a6f1469448fd9fc204 (diff) | |
download | rails-55eba62a1074a4c1e70a9b96b78b3dbf812f5e9a.tar.gz rails-55eba62a1074a4c1e70a9b96b78b3dbf812f5e9a.tar.bz2 rails-55eba62a1074a4c1e70a9b96b78b3dbf812f5e9a.zip |
Merge pull request #36760 from andre-lgf/master
[ci skip] collection_radio_buttons options example
-rw-r--r-- | guides/source/action_view_overview.md | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/guides/source/action_view_overview.md b/guides/source/action_view_overview.md index a1b69edd22..dda3ae0863 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: <label for="article_author_id_3">M. Clark</label> ``` +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 +<input id="article_author_id_1" name="article[author_id]" type="radio" value="1" /> +<label for="article_author_id_1">D. Heinemeier Hansson</label> +<input id="article_author_id_2" name="article[author_id]" type="radio" value="2" /> +<label for="article_author_id_2">D. Thomas</label> +<input id="article_author_id_3" name="article[author_id]" type="radio" value="3" checked="checked" /> +<label for="article_author_id_3">M. Clark</label> +``` + +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.value == Author.last.id}") { 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. |