rogerkk
rogerkk

Reputation: 5715

Rails (3) validation of non-ActiveRecord field

I have a model with a corresponding form, for which I use ActiveRecord validations. At the bottom of the form I'd like to have a single confirmation checkbox which should not be persisted, but which must be checked for the form to be submitted. I'd also like any errors that stems from this checkbox not being checked to display alongside the ActiveRecord errors.

Now I could cobble something together in the controller manually, but I'm wondering if there is a built-in, cleaner way to handle this kind of situation?

Upvotes: 0

Views: 428

Answers (1)

Ireneusz Skrobis
Ireneusz Skrobis

Reputation: 1533

I think you should add this in your model:

validates_acceptance_of :check_me
attr_accessor :check_me
attr_accessible :check_me # if you already have attr_accessible defined in your model

and this in your view:

<%= form_for @your_model do |f| %>
  # some code
  <%= f.check_box :check_me %>
<% end %>

Upvotes: 2

Related Questions