Changing ‘text’ in radio buttons collection from Rails form helpers
I wanted to change the ‘text’ of a radio button collection in Rails but seriously there isn’t any proper solution out there at-least i couldn’t find it anywhere. So, After some hit and trial finally got it working and I decided to share this with others.
Problem statement
First let me explain whats the issue i was facing. In my current Project there are two different role a user can have, one is ‘Vendor’ and other is ‘User’ (which is going to be like a general user). Now, I named Vendor’s role as a ‘vendor’ in table attribute, since it was pretty straight forward but on the front-end I needed to show Vendor’s role as a ‘Professional’. Here is the tripping point. If you use collection_radio_buttons helper as usual it will simply print out the value which is coming out of the Database but what if you want to change the ‘text’ to something else like in my case i wanted to print it out as a ‘Professional’ instead of ‘Vendor’.
PS: ‘text’ is the label which you see right next to your radio button.
Solution
Solution to this issue is very simple. So, all you need to do is to use conditional statement inside the collection_radio_buttons block and voilla!!..