Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to add an empty 'required' attribute to a radio_button_tag

I'm trying to use a radio_button_tag to generate a radio-button tag like:

<input type="radio" ... required>

in order to add form validation (I'm using Foundation and trying to make use of the Abide library). The closest I can get is:

radio_button_tag 'my_radio', ... , required: ''

# => <input type="radio" ... required="required">

This seems to be good enough for Abide to work, but is there a way I can get what I want out of the Rails helper? I've tried required: true in place of required: '' but it behaves the same.

like image 894
ivan Avatar asked Jul 21 '14 13:07

ivan


2 Answers

There wasn't a literal answer to copy/paste so I am providing one.

<%= radio_button_tag(:model_id, model.id, false, required: 'required') %>

like image 113
aaronbartell Avatar answered Sep 23 '22 15:09

aaronbartell


As you can see here: https://github.com/rails/rails/blob/4de8851289077239ecc91473bdba30f8cf6727bb/actionview/lib/action_view/helpers/tag_helper.rb#L149

Rails just looks for the presence of a value with "if value". A blank string is considered a value since it isn't nil and that is what triggers rails to include that "required='required'" attribute.

Does Abide really need the tag to be blank? It looks like it may just be checking for the presence of the "required" attribute so it may not care if the attribute has a value or not.

like image 38
Curtis Edmond Avatar answered Sep 22 '22 15:09

Curtis Edmond