【问题标题】:How to set boolean values to radio buttons in rails 4 form如何将布尔值设置为rails 4形式的单选按钮
【发布时间】:2016-06-16 20:30:51
【问题描述】:

我正在尝试为 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"}

【问题讨论】:

  • 你在哪里得到错误值?当您提交表单时,您的参数哈希中有什么?
  • 我在问参数哈希,当您提交表单时实际值是多少 - puts params
  • 我的错。修复了我的编辑

标签: ruby-on-rails


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    问题已解决

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

    【讨论】:

    • 不是两个标签都附在第一个单选按钮上吗?
    • 当前不接受正确答案,接受此错误答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多