Rails 应用程序单选按钮不更新布尔字段

Rails application radio buttons do not update boolean field

我有一个基本的 Rails 表单(使用 rails-bootstrap-forms gem 来格式化)我想要单选按钮来设置布尔字段要制作的对象。这是代码片段(为简洁起见缩短):

<%= bootstrap_form_for(@comment) do |f| %>

  <%= f.text_field :name, label: "Name (required)", label_class: "whiteClass" %>
  <%= f.text_field :email, label_class: "whiteClass" %>
  <%= f.form_group :public  do %>
    <%= f.radio_button :public, true, label: "Public", label_class: "whiteClass", inline: true, checked: true %>
    <%= f.radio_button :public, false, label: "Private (Only Paul will see)", label_class: "whiteClass", inline: true %>
  <% end %>
  <%= f.submit "Send" %>

<% end %>

我试图让单选按钮从原始 rails 表单中响应,但没有成功。任何帮助将非常感激。谢谢

尝试指定 "true""false" 而不是 truefalse:

<%= f.form_group :public  do %>
  <%= f.radio_button :public, "true", label: "Public", label_class: "whiteClass", inline: true, checked: true %>
  <%= f.radio_button :public, "false", label: "Private (Only Paul will see)", label_class: "whiteClass", inline: true %>
<% end %>

希望对您有所帮助!

您可能想看看 this

我想你只需要添加

validates_inclusion_of :public, :in => [true, false]

尝试为单选按钮添加一个值:

<%= f.radio_button :public, true, label: "Public", label_class: "whiteClass", value: true, inline: true, checked: true %>
<%= f.radio_button :public, false, label: "Private (Only Paul will see)", label_class: "whiteClass", value: false, inline: true %>