I'm adding a checkbox to a sign up page that requires a user check a box agreeing to terms of use. Checking the box is supposed to be required to sign up.
The problem is that when the box is checked and submit is clicked, the message says user must check the box. As a result user can't sign up.
So it looks like there is 2 ways to do the checkbox for terms of service - with and without a distinct db column. I went the db column route which is probably harder but I already had done that part.
This is what I know ... I can see terms_accepted is now in the users table as a boolean. All previous users terms_accepted marked FALSE which makes sense.
I've read lots of comments online about this, and tried about 10 flavors of the validation snippet without luck. It may have something about Rails 4 that I'm missing. What I read online says if you do have a db column for this, DO use an accept option. If you do have a db column DO NOT use the validates_acceptance_of in your user.rb.
I followed the Ruby doc for validations in section 2.1 says to do it like this:
class Person < ActiveRecord::Base
validates :terms_of_service, acceptance: { accept: 'yes' }
end
This is my user controller and model:
class UsersController < ApplicationController
before_filter :authenticate_user!
before_filter :admin_only, :except => :show
def index
@users = User.all
end
def show
@user = User.find(params[:id])
unless current_user.admin?
unless @user == current_user
redirect_to :back, :alert => "Access denied."
end
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
user = User.find(params[:id])
user.destroy
redirect_to users_path, :notice => "User deleted."
end
private
def user_params
params.require(:user).permit(:name, :email, :terms_accepted)
end
def admin_only
unless current_user.admin?
redirect_to :back, :alert => "Access denied."
end
end
def secure_params
params.require(:user).permit(:role)
end
end
---
class User < ActiveRecord::Base
validates :terms_accepted, acceptance: { accept: 'yes' }
enum role: [:user, :vip, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
end
---
Here is the views/devise/registrations/new (View for Sign Up form)
<div class="row">
<div class="col-md-6">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
<h2 class="form-signin-heading">Sign Up</h2>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :confirm_password %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.check_box :terms_accepted%>
<%= f.label :accept_terms %>
<%= link_to 'Terms Of Use', '/TermsOfUse.html', :target => "_blank" %>
</div>
<div class="form-group">
<%= f.submit 'Sign Up', :class => 'btn btn-lg btn-login' %>
</div>
<div class="form-group">
<%= render "devise/shared/links" %>
</div>
<% end %>
</div>
Output:
Started GET "/" for ::1 at 2015-06-29 13:55:55 -0700 Processing by VisitorsController#index as HTML (0.5ms) SELECT COUNT(*) FROM "users" Rendered visitors/index.html.erb within layouts/application (1.6ms) Rendered layouts/_flatlabnavbartop.html.haml (16.2ms) Completed 200 OK in 347ms (Views: 345.7ms | ActiveRecord: 0.5ms)
Started GET "/users/sign_up" for ::1 at 2015-06-29 13:56:00 -0700 Processing by DeviseInvitable::RegistrationsController#new as HTML Rendered /usr/local/rvm/gems/ruby-2.2.1@suits6/gems/devise-3.4.1/app/views/devise/shared/_links.html.erb (1.4ms) Rendered devise/registrations/new.html.erb within layouts/application (51.9ms) Rendered layouts/_flatlabnavbartop.html.haml (1.9ms) Completed 200 OK in 279ms (Views: 277.9ms | ActiveRecord: 0.0ms)
Started POST "/users" for ::1 at 2015-06-29 13:56:29 -0700 Processing by DeviseInvitable::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"4t0UPaQhI/0HcsqC3RkBrcWQWjhWzKojZLvrMloObPSAiapVc46bvxT5TGePh4v2IUCi8QbdVuMWuQsyzyFmdg==", "user"=>{"name"=>"Maude Username", "email"=>"maude@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_accepted"=>"1"}, "commit"=>"Sign Up"}
Unpermitted parameter: terms_accepted
User Load (119.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 AND "users"."encrypted_password" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["email", "maude@gmail.com"], ["encrypted_password", ""]]
(0.2ms) BEGIN User Exists (16.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'maude@gmail.com' LIMIT 1
(8.3ms) ROLLBACK Rendered /usr/local/rvm/gems/ruby-2.2.1@suits6/gems/devise-3.4.1/app/views/devise/shared/_links.html.erb (0.7ms)
Rendered devise/registrations/new.html.erb within layouts/application (18.5ms)
Rendered layouts/_flatlabnavbartop.html.haml (2.9ms)
Completed 200 OK in 733ms (Views: 383.5ms | ActiveRecord: 144.9ms)
TL;DR: It is doing a rollback and saying unpermitted parameters for terms_aacpeted is stopping it. But for sure I have those in the user_params in the UsersController.
What needs to be changed?