I am trying to add a terms of service checkbox to my devise invitable view. So an invite is sent out and the Engineer clicks the accept invite link and arrives at the form below:
<h2><%= t 'devise.invitations.edit.header' %></h2>
<%= simple_form_for resource, as: resource_name, url: invitation_path(resource_name), html: { method: :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.input :terms_of_service, as: :boolean, required: true, label: ("I agree to the #{link_to 'Terms of Service', page_path('terms'), target: :_blank}").html_safe %>
<%= f.button :submit, t("devise.invitations.edit.submit_button") %>
<% end %>
My model is:
class Engineer < ApplicationRecord
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessor :terms_of_service
.
.
.
validates_acceptance_of :terms_of_service, if: :invitation_accepted_at?, acceptance: true, allow_nil: false
I have a custom devise controller:
class CustomDeviseController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
.
.
.
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name])
devise_parameter_sanitizer.permit(:accept_invitation, keys: [:first_name, :last_name, :terms_of_service])
end
I get the form error "Terms of service must be accepted", which tells me the validations are running. The problem appears to be that the terms of service fails as it is nil and so the validations fail. I can't see why when I tick the checkbox it doesn't pass that and meet the validations?
--- UPDATED ---
The params hash generated is:
{"utf8"=>"✓", "authenticity_token"=>"aoGCMoYRcmJ2EmpM8L+UmQQcB99FScgA6nFM0kY+53dxDodCZDislej48+lBL4Qcb5ZgAcGHyd1A+3qkIol6KA==", "engineer"=>{"invitation_token"=>"e9oVkxuYMtX9Fzxao7xR", "first_name"=>"sad", "last_name"=>"test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_of_service"=>"0"}, "commit"=>"Create Account"}
Unpermitted parameter: :terms_of_service
Turns out because I had a customer devise controller but was also name spacing the invitables stuff under Engineers. I was permitting the params in the wrong file.
I had to permit them in the Engineers::InvitationsController instead.