diff options
author | André Galatti Faria <andreluisgalatti@hotmail.com> | 2019-07-24 22:50:11 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-24 22:50:11 -0300 |
commit | 1a0e9f5125e62dd441f2901270db9a1f2c9c96b9 (patch) | |
tree | 3a4b333e2c802b0198754265b38a794313d54bcf /guides/source | |
parent | 49b531ba588eb8da50ee810b377a461dc9aee618 (diff) | |
download | rails-1a0e9f5125e62dd441f2901270db9a1f2c9c96b9.tar.gz rails-1a0e9f5125e62dd441f2901270db9a1f2c9c96b9.tar.bz2 rails-1a0e9f5125e62dd441f2901270db9a1f2c9c96b9.zip |
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.
Diffstat (limited to 'guides/source')
-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..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: <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.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. |