6

我正在尝试在 rails 4 的表单中为单选按钮设置真/假值。我在 stackoverflow 上找到了一篇文章并相应地实现了我的单选按钮,但我总是将 false 作为值。

我的代码

  <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 %>

是否有不同的方法来设置 rails 4 中的单选按钮的值?

编辑:

在我的控制器中

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

参数:

{"utf8"=>"✓",
 "authenticity_token"=>"t/da2RRBi4KsyndnHx4WNZLoOHu9DVlAWtl/59NPiMc=",
 "accessor"=>{"accessor_id"=>"email",
 "access_rights"=>"Read and Write"},
 "commit"=>"Grant Permission"}
4

2 回答 2

7

我相信其他答案的标签不会正常工作。这是一个更正标签的示例。

<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>
于 2016-06-16T12:30:51.947 回答
3

问题已解决

 <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>
于 2013-10-12T21:38:20.000 回答