Rails using forms arrays with inclusion validation

Trying to get good advice on the best approach to using arrays to select a form, but using the same array to check for inclusion for validation.

Now I have this, but building arrays inside the elements and checking, i.e.

# Form <%= f.select(:status, [['Live','live'], ['Paused', 'paused']]) %> # Model validates :status, :inclusion => { :in => %w(live paused) } 

I am sure there will be a better way to store these arrays and use them!

Thanks for any advice you can provide.

+4
source share
1 answer

You can add these two constants to your model and then call confirmation:

 VALID_STATES = ["live", "paused"] SELECT_STATES = VALID_STATES.map { |s| [s.capitalize, s] } validates :status, :inclusion => { :in => Model::VALID_STATES } 
+10
source

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


All Articles