Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 验证rails模型中的单选按钮和复选框 概述_Ruby On Rails_Forms_Validation_Ruby On Rails 4_Checkbox - Fatal编程技术网

Ruby on rails 验证rails模型中的单选按钮和复选框 概述

Ruby on rails 验证rails模型中的单选按钮和复选框 概述,ruby-on-rails,forms,validation,ruby-on-rails-4,checkbox,Ruby On Rails,Forms,Validation,Ruby On Rails 4,Checkbox,在我看来,我有两个单选按钮表单帮助程序:Yes和No 我还有一个带有标识符的复选框:agreement 这是我的密码: %h6 Is this a membership? = f.radio_button :agreement, true, id: "membership_yes" = f.label :agreement, "Yes", value: true = f.radio_button :agreement, false, id:

在我看来,我有两个
单选按钮
表单帮助程序:
Yes
No

我还有一个带有标识符的
复选框
:agreement

这是我的密码:

%h6 Is this a membership?   

= f.radio_button :agreement, true, id: "membership_yes" 

= f.label :agreement, "Yes", value: true

= f.radio_button :agreement, false, id: "membership_no" 

= f.label :agreement, "No", value: false

%br/ 

= f.check_box :agreement, type: "checkbox", id: "agreement-box"
= f.label :agreement, label: "I acknowledge that I have completed at least 8 workouts for the month."
我想要完成的 在我的rails模型中,我希望对
:agreement
复选框执行验证

如果选择了
Yes
,则显示复选框并将其设为必填项。如果选择了
No
,则隐藏该复选框并将其设置为非必需

我非常感谢任何能为我指明正确方向的帮助或资源

我试过的 我尝试过在使用Jquery隐藏、显示复选框的
required
属性时为单选按钮和复选框
id
,并将其设置为
false
true

我只能显示和隐藏复选框。当将
required
属性设置为
true
false
时,无论我按下
Submit
按钮时发生什么情况,表单都会被忽略并提交


如果有更好的方法来组织我的代码,我将不胜感激。提前谢谢

首先,为模型中的单选按钮添加属性访问器

attr_accessor :show_agreement
改变你的形式

%h6 Is this a membership?   

= f.radio_button :show_agreement, true, id: "membership_yes", name: "agreement_radio_buttons" 

= f.label :show_agreement, "Yes", value: true

= f.radio_button :show_agreement, false, id: "membership_no", name: "agreement_radio_buttons" 

= f.label :show_agreement, "No", value: false

%br/

#agreement-box
   = f.check_box :agreement, type: "checkbox"
   = f.label :agreement, label: "I acknowledge that I have completed at least 8 workouts for the month."
单选按钮更改时显示/隐藏复选框

$(document).ready(function(){ 
    $("input[name$='agreement_radio_button']").click(function() {
        var showCheckBox = $(this).val();
        if (showCheckBox) {
            $("#agreement-box").show();
        } else {
            $("#agreement-box").hide();
            $("#agreement-box input").prop('checked', false);
        }
    }); 
});
最后,在模型中添加验证

validates :agreement, acceptance: {accept: true}, if: :needs_agreement_acceptance?

private

def needs_agreement_acceptance?
    show_agreement
end
我还没试过,但我想你会没事的。 此外,根据单选按钮的预选,确保在(重新)加载页面时显示或隐藏协议框

关于不让表单提交的信息

您可以禁用submit按钮,并选中when复选框以查看更改。如果为真,请删除禁用属性

$('#agreement_box').on('change', 'input', function() {
    if (self.is(":checked")) {
        $('#submit-btn').removeAttr('disabled')
    } else {
        ...
    }
});

对不起,我没有说得更透彻

如果您使用布尔值,您使用单选按钮
有什么用?只需使用
复选框
!在尝试提交表单时,rails给我一个错误,指出
未定义的方法“需要\u同意\u接受”
而且如果选中了
Yes
,而复选框未选中,我甚至不能提交表单。谢谢你的帮助!我会很感激任何额外的输入,因为我已经为此奋斗了很长一段时间。检查拼写错误,我没有发现任何错误。(供参考:)。