Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept terms of use rails

What is the best way to add a check for accepting terms of use in a rails app?

I can't seem to get validates_acceptance_of working quite right. I added a bool to my user model (was that necessary?). And then have a checkbox that returns either true/false.

I feel like I'm just making a silly little mistake. Any ideas?

like image 524
Cyrus Avatar asked Jun 12 '11 19:06

Cyrus


2 Answers

In your model,

validates_acceptance_of :terms

If you're using attr_accessible in your model then make sure you also add,

attr_accessible :terms

In your view,

<%= form_for @user do |f| %>
  ...
  <%= f.check_box :terms %>
  ...
<% end %>

There is no need for an extra column in the users table unless you plan on denying access to users who have not accepted the terms of service, which won't exist since they can't complete registration in the first place.

like image 125
Aldehir Avatar answered Nov 16 '22 01:11

Aldehir


This is a working Rails 4 solution:

Terms of service doesn't need to be a column in the database

Form

= f.check_box :terms_of_service

models/user.rb

validates :terms_of_service, acceptance: true

And most important, devise will sanitize your parameters and terms of service will be removed from the submitted params. So:

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:full_name,
        :email, :password, :password_confirmation, :terms_of_service)
    end 
  end
end
like image 36
vladCovaliov Avatar answered Nov 16 '22 03:11

vladCovaliov