【问题标题】:Rails application radio buttons do not update boolean fieldRails 应用程序单选按钮不更新布尔字段
【发布时间】:2015-09-20 12:54:00
【问题描述】:

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

<%= 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 表单,但没有成功。任何帮助将非常感激。谢谢。

【问题讨论】:

  • public 是否包含在您的强参数中?如果是这样,它们都不起作用,或者您是否遇到无法从 true 更改为 false 的问题?
  • 我能够使用 Jake 的回答解决问题。谢谢大家的帮助!

标签: ruby-on-rails radio-button form-submit


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试指定"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 %>
    

    希望有帮助!

    【讨论】:

      【解决方案3】:

      你可能想看看this

      我想你只需要添加

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

      【讨论】:

        猜你喜欢
        • 2014-01-20
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多