User request to select a value

I have a selection menu like

<%= f.select(:size, options_for_select(@sizes_availiable), :prompt => "Select Size") %>

With the prompt "Choose size". The problem is that the user can select "Choose size", and he will still allow to submit the form. How to prevent the user from choosing this value as a value, given that all sizes are also strings?

+1
source share
3 answers

There are a few things you can do:

# as suggested, use :include_blank
f.select :size, options_for_select(@sizes_availiable), include_blank: "Select Size"
# wihch creates an 'option' tag with no value and 'Select Size' text

# or/and add a validation
validates :size, presence: true
validates :size, numericality: true       # or
validates :size, format: { with: /\d+/ }  #

However, you should always include checks so that attackers do not manipulate the form or send random things.

+4
source

Use include_blankinstead prompt.

 <%= f.select(:size, options_for_select(@sizes_availiable), {:include_blank => "Select Size"}) %>

Than you can check sizein your model.

validates :size,   :presence => true
+3

Usage: Disabled

<%= f.select(:size, options_for_select(@sizes_availiable), :prompt => "Select Size", :disabled => 'Select Size') %>

Link:

http://zittlau.ca/ruby-on-rails-disabling-a-select-tag-using-the-select-helper/

+2
source

Source: https://habr.com/ru/post/1533845/


All Articles