DEV Community

Cover image for How to Add ToS Agreement Checkbox to Your Rails App using Devise?
Zil Norvilis
Zil Norvilis

Posted on • Updated on

How to Add ToS Agreement Checkbox to Your Rails App using Devise?

In this article, I will show you how to add functionality to your new registration view, which will force the user to agree to your app's Terms of Service (ToS) when creating new account.

I am using these gems:

  • Rails 6.1.4.1
  • Devise 4.8
  • Simple Form 5.1
  • Haml 5.2

I presume that you already have Devise and your User model setup and ready to go.

STEP 1
First off, if you haven't already done it, generate Devise registration controller with this command:

rails g devise:controllers users -c=registrations

and let your routes know about it:

# routes.rb

devise_for :users, controllers: {
      registrations: 'users/registrations'
  }
Enter fullscreen mode Exit fullscreen mode

and generate your new registration view, where you will insert ToS agreement checkbox input:

rails g devise:views -v registrations

STEP 2
Now you need to add new column to users table in your database schema, with migration like this:
rails g migration AddTosAgreementToUsers tos_agreement:boolean

the migration file is gonna look like this:

# 20220108144502_add_tos_agreement_to_users.rb

class AddTosAgreementToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :tos_agreement, :boolean
  end
end
Enter fullscreen mode Exit fullscreen mode

migrate the change with rails db:migrate

STEP 3
Add the following to your User model so it forces ToS agreement to be checked before allowing to successfully submit registration form. Also make sure to include on: :create, so this checkup is not forced on when user is just updating the profile.

# users.rb

validates_acceptance_of :tos_agreement, allow_nil: false, on: :create
Enter fullscreen mode Exit fullscreen mode

STEP 4
Permit the 'tos_agreement' param

# users/registrations_controller.rb

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:tos_agreement])
  end
Enter fullscreen mode Exit fullscreen mode

STEP 5
Add the checkbox input to your new registration view. I use link_to method to insert a link leading to my pre-created ToS page.
Terms of Service Checkbox Preview

# views/devise/registrations/new.html.haml

= f.input :tos_agreement, as: :boolean,
         label: "I agree to #{link_to 'Terms of Service',
           terms_of_service_path, target: '_blank'}".html_safe
Enter fullscreen mode Exit fullscreen mode

That's it, now you should have a nice functionality that will force your app users to agree to your terms before creating new account.

Discussion (2)

Collapse
raynawara profile image
Raymond J Nawara • Edited on

I really liked your tutorial but can you show your ToS code, view and route, for the page to create a checkbox?

Collapse
zilton7 profile image
Zil Norvilis Author • Edited on

Sure, here you go gist.github.com/zilton7/f4e6b50abd...
and route just contains a simple:
get '/privacy-policy', to: 'static#privacy_policy'