简体   繁体   English

Rails:获取f.range_field提交null或nil作为默认值

[英]Rails: get f.range_field to submit null or nil as default value

In my app, I have a range_field where a user can select 0..100. 在我的应用程序中,我有一个range_field,用户可以在其中选择0..100。 But if they don't select anything, I want the form to submit "null" or nil as the value rather than a numerical value. 但是,如果他们什么都没选择,我希望表单提交“ null”或nil作为值而不是数字值。

Is this possible? 这可能吗?

If I use the below syntax, then everything works except value is not nil: 如果我使用以下语法,那么除了value不为nil之外,其他所有东西都可以工作:

 <%= f.range_field :content, :value=> nil, :class=> "question-field percentage", :step => 10, :in => 0..100, :data => {:question => question.id} %> 

If I use this syntax, then the value is nil but my other options don't work: 如果我使用此语法,则该值为nil,但其他选项无效:

 <%= f.range_field :content, :value=> nil, :options=> {:in => 0..100, :step => 10, :class=> "question-field percentage", :data => {:question => question.id}} %>

You can just add a hidden checkbox value to judge whether user select or not. 您可以仅添加一个隐藏的复选框值来判断用户是否选择。

= f.range_field :content, :value=> nil, :class=> "question-field percentage", :step => 10, :in => 0..101, :data => {:question => question.id
= check_box_tag :range_clicked, true, false, style:'display:none;'

and then add a js function to tell whether user clicked range input or not 然后添加一个js函数来判断用户是否点击了范围输入

  $('#foobar_content').on('click',function(){
    $('#range_clicked').attr('checked',true);
  });

if user select range input , the value of params[:range_clicked] is true , otherwise , it's nil . 如果用户选择范围输入,则params[:range_clicked]值为true,否则为nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM