How to do checkbox validation in Ruby on Rails?

0

I'm studying ruby on rails for a while, and one of the exercises I'm trying to do is a checkbox and a submit button on a haml page, the purpose is for the submit button to work only if the checkbox is checked, otherwise just give a generic message like "Complete the fields", how can I do this?

    
asked by anonymous 06.10.2018 / 17:00

1 answer

1

Rails has a validator named acceptance_of . It is used when you have a boolean and want to check if this value is true.

class Person < ApplicationRecord
  validates :terms_of_service, acceptance: true
end

See more Rails validations at documentation .

When you call model.save , check the following:

if person.save
  # successo!
else
  # erro :(
  person.errors.full_messages
  # => [ { terms_of_service: "must be accepted" }]
end
    
06.10.2018 / 17:05