我遇到了使用设计和简单表格验证我们的服务条款复选框的问题。
用户模型
validates_acceptance_of :terms, :allow_nil => false, :message => :terms_not_accepted, :on => :create
注册视图
= f.label :terms, "I agree to the #{link_to 'Terms of Service', "http://britevid.com/terms_of_service",:remote => true}.".html_safe, {class: "checkbox inline"}
= f.check_box :terms
= f.button :submit, 'Sign up', :class => 'btn btn-success'
应用程序控制器
def update_sanitized_params
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:first_name, :last_name, :company, :phone_number, :website, :content_description, :address, :city, :state, :zip_code, :country, :paypal_email, :time_zone, :email, :current_password, :password, :password_confirmation, :terms)}
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:first_name, :last_name, :company, :phone_number, :website, :content_description, :address, :city, :state, :zip_code, :country, :paypal_email, :time_zone, :email, :current_password, :password, :password_confirmation)}
end
Started POST "/users" for 127.0.0.1 at 2014-03-21 07:19:24 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "user"=>{"first_name"=>"jeff", "last_name"=>"smith", "email"=>"jeffsmith@lol.com", "company"=>"test", "phone_number"=>"453234562", "website"=>"", "content_description"=>"test", "address"=>"1st ave", "city"=>"big city", "state"=>"California", "country"=>"United States", "zip_code"=>"095610", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms"=>"0"}, "commit"=>"Sign up"}
(0.2ms) BEGIN
User Exists (0.1ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'jeffsmith@lol.com' LIMIT 1
(0.1ms) ROLLBACK
Rendered devise/registrations/new.html.haml within layouts/pages (46.2ms)
Rendered layouts/shared/_external_header.html.haml (0.4ms)
Completed 200 OK in 157ms (Views: 67.7ms | ActiveRecord: 0.4ms)
答案 0 :(得分:1)
您已在terms
validates_acceptance_of :terms, :allow_nil => false, :message => :terms_not_accepted, :on => :create
因此,在创建新的terms
时,必须选中User
复选框。如果您没有选中该复选框,那么验证将失败,您将收到错误消息。并且当然用户记录没有得到保存。
根据服务器日志,在提交表单时,您没有选择terms
复选框为"terms"=>"0"
(以params哈希表示)。
确保在提交表单之前选择条款。