Accept terms of use rails

前端 未结 2 1097
北海茫月
北海茫月 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:05

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题