Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set boolean values to radio buttons in rails 4 form

I am trying to set a true/false value to a radio button in a form in rails 4. I found a post on stackoverflow and implemented my radio buttons accordingly but I always get false as a value.

my code

  <div><%= label :access_rights, 'Read Only', :value => "false" %></div>
  <%= f.radio_button :access_rights, "Read Only", :checked => true , false%></div>
  <div><%= label :access_rights, 'Read and Write', :value => "true" %></div>
  <%= f.radio_button :access_rights, "Read and Write", true %>

Is there a different way to set values for a radio button in rails 4?

EDIT:

in my controller

def access_params
  params.require(:accessor).permit(:email, :access_rights)
end

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"t/da2RRBi4KsyndnHx4WNZLoOHu9DVlAWtl/59NPiMc=",
 "accessor"=>{"accessor_id"=>"email",
 "access_rights"=>"Read and Write"},
 "commit"=>"Grant Permission"}
like image 582
Quantico Avatar asked Oct 12 '13 20:10

Quantico


2 Answers

I believe the other answer's labels wouldn't work right. Here's an example with the labels corrected.

<div>
  <%= label :access_rights, "Read Only", value: false %>
  <%= f.radio_button :access_rights, false, :checked => true, :value => false %>
</div>

<div>
  <%= label :access_rights, "Read and Write", value: true %>
  <%= f.radio_button :access_rights, true, :value => true%>
</div>
like image 186
Jason Swett Avatar answered Sep 28 '22 01:09

Jason Swett


Issue was resolved

 <div><%= label :access_rights, "Read Only" %>
      <%= f.radio_button :access_rights, false , :checked => true , :value => false %></div>

  <br>
  <div><%= label :access_rights, "Read and Write"%>
       <%= f.radio_button :access_rights, true, :value => true%></div>
like image 41
Quantico Avatar answered Sep 28 '22 01:09

Quantico