【问题标题】:Radio button not working in rails form单选按钮不能以导轨形式工作
【发布时间】:2015-06-13 01:41:14
【问题描述】:

我想创建一个包含 2 个单选按钮的表单。一个用于“公共”,另一个用于“私人”。

我的项目中有一个用户模型,其中有一个隐私字段,它是布尔值,其默认值为 true(在我的情况下为“公共”)。

我的表单中有更新按钮。我想提交表单,因此想更新用户模型中的值。然后如果每当我访问编辑页面时,应该选择相应的单选按钮。

这是我的表格

= form_tag "/users/abc" do
    %p.heading We Respect Your Privacy
    .privacy
      %input#r1{:name => "privacy", :type => "radio", :value => 0}
        %label.privacyLabel{:for => "r1"}
          %span>
          Friends Only
        %p
          TraveLibro enables you to save your travel memories and share them with your friends. While other TraveLibro users
          can see your itineraries, only your friends on TraveLibro can view your personal pictures.
        %br
          %p Now you can upload all your captured memories without ever having to be concerned about your privacy.
    .privacy
      %input#r2{:name => "privacy", :type => "radio", :value => 1}
        %label.privacyLabel{:for => "r2"}
          %span>
          Everyone
        %p
          Want to share your travel tales with the world. With an open profile TraveLibro users can view the itineraries
          you have created and the pictures that complete those memories.
    .privacyUpdate
      %a{:href => "javascript:void(0);"}
        %input.privacyUpdateBtn{:type => "submit", :value => "update"}/

这是我的控制器操作

def abc
  current_user.update_column('privacy', params[:privacy] == 0 ? 0 : 1)
  redirect_to :back
end

但它不起作用

【问题讨论】:

  • 检查params[:privacy] == '0' ? 0 : 1

标签: ruby-on-rails-4


【解决方案1】:

试试这个:

def abc
  current_user.update_column('privacy', params[:privacy].to_i == 0 ? 0 : 1)
  redirect_to :back
end

【讨论】:

    猜你喜欢
    • 2010-10-19
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2018-01-17
    • 2011-06-25
    相关资源
    最近更新 更多