1
我想将服务条款复选框添加到我的设计可邀请视图。 所以邀请被发送出去,工程师点击接受邀请链接,并在下面的表格到达:设计应用服务条款
<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 %>
我的模式是:
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
我有一个自定义色器件控制器:
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
我收到表单错误“服务条款必须被接受”,它告诉我验证正在运行。问题似乎是服务条款因为无效而失败,所以验证失败。我看不到为什么当我勾选复选框时,它没有通过并满足验证?
---修订---
params哈希表生成的是:
{"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
你能发表正在生成的表单提交的参数哈希值吗? – Pavan
嗯,我看到触发的动作是更新。尝试将':terms_of_service'放入'account_update' params – Pavan
仍然没有运气。如果我删除了validates_acceptance_of上的allow_nil:false,即使复选框未选中,它也会传递 – rmaspero