Accept terms of use rails

前端 未结 2 1106
北海茫月
北海茫月 2021-02-05 19:36

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 b

2条回答
  •  北海茫月
    2021-02-05 20:08

    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
    

提交回复
热议问题