简体   繁体   中英

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. But if they don't select anything, I want the form to submit "null" or nil as the value rather than a numerical value.

Is this possible?

If I use the below syntax, then everything works except value is not 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:

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

  $('#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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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