{"rsdb":{"rid":"377801","subhead":"","postdate":"0","aid":"264311","fid":"87","uid":"1","topic":"1","content":"
\n

acceptance\u9a8c\u8bc1<\/h3> \n

acceptance<\/code> \u662f Rails \u4e2d\u7684\u4e00\u4e2a\u9a8c\u8bc1\u5668\uff08validator\uff09\uff0c\u7528\u4e8e\u9a8c\u8bc1\u4e00\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u5c5e\u6027\u662f\u5426\u88ab\u63a5\u53d7\u3002\u5728\u8868\u5355\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e9b\u590d\u9009\u6846\u6216\u5355\u9009\u6309\u94ae\uff0c\u7528\u6237\u9700\u8981\u52fe\u9009\u6216\u9009\u62e9\u624d\u80fd\u63d0\u4ea4\u8868\u5355\u3002acceptance<\/code> \u9a8c\u8bc1\u5668\u7528\u4e8e\u786e\u4fdd\u8fd9\u4e9b\u590d\u9009\u6846\u6216\u5355\u9009\u6309\u94ae\u5df2\u7ecf\u88ab\u9009\u4e2d\u6216\u52fe\u9009\u3002<\/p> \n

\u5f53\u4e00\u4e2a\u5c5e\u6027\u88ab\u9a8c\u8bc1\u65f6\uff0cacceptance<\/code> \u9a8c\u8bc1\u5668\u4f1a\u68c0\u67e5\u8be5\u5c5e\u6027\u662f\u5426\u4e3a true\u3002\u5982\u679c\u4e3a true\uff0c\u5219\u9a8c\u8bc1\u901a\u8fc7\uff1b\u5426\u5219\uff0c\u9a8c\u8bc1\u5931\u8d25\uff0c\u5e76\u5c06\u9519\u8bef\u4fe1\u606f\u6dfb\u52a0\u5230\u6a21\u578b\u5bf9\u8c61\u7684 errors<\/code> \u96c6\u5408\u4e2d\u3002<\/p> \n

\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0cacceptance<\/code> \u9a8c\u8bc1\u5668\u7528\u4e8e\u9a8c\u8bc1 terms_of_service<\/code> \u5c5e\u6027\u662f\u5426\u88ab\u63a5\u53d7\uff1a<\/p> \n

class Person < ApplicationRecord\n  validates :terms_of_service, acceptance: true\nend\n<\/code><\/pre> \n 

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a Person<\/code> \u5bf9\u8c61\u7684 terms_of_service<\/code> \u5c5e\u6027\u6ca1\u6709\u88ab\u8bbe\u7f6e\u4e3a true\uff0c\u90a3\u4e48\u8be5\u5bf9\u8c61\u5c31\u65e0\u6cd5\u901a\u8fc7\u9a8c\u8bc1\uff0c\u5e76\u4e14\u4f1a\u5728 errors<\/code> \u96c6\u5408\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a terms_of_service<\/code> \u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p> \n

\u5728\u8868\u5355\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Rails \u63d0\u4f9b\u7684 check_box<\/code> \u6216 radio_button<\/code> \u8f85\u52a9\u65b9\u6cd5\u6765\u751f\u6210\u590d\u9009\u6846\u6216\u5355\u9009\u6309\u94ae\uff0c\u5e76\u81ea\u52a8\u6dfb\u52a0 acceptance<\/code> \u9a8c\u8bc1\u5668\u3002\u4f8b\u5982\uff1a<\/p> \n

<%= form_for @person do |f| %>\n  <%= f.label :terms_of_service do %>\n    <%= f.check_box :terms_of_service %>\n    I agree to the terms of service\n  <% end %>\n<% end %>\n<\/code><\/pre> \n 

\u8fd9\u4e2a\u8868\u5355\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3a terms_of_service<\/code> \u7684\u590d\u9009\u6846\uff0c\u5e76\u81ea\u52a8\u5c06\u5176\u5173\u8054\u5230 Person<\/code> \u6a21\u578b\u7c7b\u7684 terms_of_service<\/code> \u5c5e\u6027\u3002\u5f53\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u5982\u679c\u590d\u9009\u6846\u88ab\u9009\u4e2d\uff0c\u90a3\u4e48\u8be5\u5c5e\u6027\u4f1a\u88ab\u8bbe\u7f6e\u4e3a true\uff0c\u5e76\u4e14\u8868\u5355\u6570\u636e\u5c31\u53ef\u4ee5\u88ab\u63d0\u4ea4\u3002\u5982\u679c\u590d\u9009\u6846\u6ca1\u6709\u88ab\u9009\u4e2d\uff0c\u90a3\u4e48\u8868\u5355\u6570\u636e\u65e0\u6cd5\u901a\u8fc7\u9a8c\u8bc1\uff0c\u5e76\u4e14\u4f1a\u663e\u793a\u4e00\u4e2a\u540d\u4e3a terms_of_service<\/code> \u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p> \n

validates_associated<\/h3> \n

\u5f53\u60a8\u7684\u6a21\u578b\u4e0e\u5176\u4ed6\u6a21\u578b\u6709\u5173\u8054\uff0c\u5e76\u4e14\u5b83\u4eec\u4e5f\u9700\u8981\u9a8c\u8bc1\u65f6\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528\u8fd9\u4e2a\u5e2e\u52a9\u5668\u3002\u5f53\u4f60\u8bd5\u56fe\u4fdd\u5b58\u5bf9\u8c61\u65f6\uff0c\u6709\u6548\u5417?\u5c06\u5728\u6bcf\u4e2a\u76f8\u5173\u5bf9\u8c61\u4e0a\u8c03\u7528\u3002<\/p> \n

class Library < ApplicationRecord\n  has_many :books\n  validates_associated :books\nend\n<\/code><\/pre> \n 

\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a Rails \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 ActiveRecord \u6a21\u578b\u7c7b\uff0c\u5b83\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Library<\/code> \u7684\u7c7b\uff0c\u5e76\u5efa\u7acb\u4e86\u4e0e Book<\/code> \u6a21\u578b\u7c7b\u7684\u5173\u8054\u5173\u7cfb\u3002\u5b83\u4f7f\u7528\u4e86 has_many<\/code> \u65b9\u6cd5\u6765\u6307\u5b9a\u4e00\u4e2a\u56fe\u4e66\u9986\u53ef\u4ee5\u62e5\u6709\u591a\u672c\u4e66\uff0c\u5e76\u4f7f\u7528\u4e86 validates_associated<\/code> \u65b9\u6cd5\u6765\u786e\u4fdd\u4e0e\u8be5\u6a21\u578b\u5173\u8054\u7684\u4e66\u7c4d\u6570\u636e\u4e5f\u662f\u6709\u6548\u7684\u3002<\/p> \n

has_many<\/code> \u65b9\u6cd5\u7528\u4e8e\u5728\u6a21\u578b\u7c7b\u4e4b\u95f4\u5efa\u7acb\u4e00\u5bf9\u591a\u7684\u5173\u8054\u5173\u7cfb\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cLibrary<\/code> \u6a21\u578b\u7c7b\u901a\u8fc7 has_many :books<\/code> \u65b9\u6cd5\u6307\u5b9a\u4e00\u4e2a\u56fe\u4e66\u9986\u53ef\u4ee5\u62e5\u6709\u591a\u672c\u4e66\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u81ea\u52a8\u4e3a Library<\/code> \u7c7b\u751f\u6210\u4e00\u4e2a\u540d\u4e3a books<\/code> \u7684\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u8fd4\u56de\u4e0e\u8be5\u56fe\u4e66\u9986\u76f8\u5173\u8054\u7684\u6240\u6709\u4e66\u7c4d\u7684\u96c6\u5408\u3002\u540c\u65f6\uff0c\u5b83\u8fd8\u4f1a\u81ea\u52a8\u4e3a Book<\/code> \u7c7b\u751f\u6210\u4e00\u4e2a\u540d\u4e3a library<\/code> \u7684\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u8fd4\u56de\u4e0e\u8be5\u4e66\u7c4d\u76f8\u5173\u8054\u7684\u56fe\u4e66\u9986\u5bf9\u8c61\u3002<\/p> \n

validates_associated<\/code> \u65b9\u6cd5\u7528\u4e8e\u9a8c\u8bc1\u4e0e\u8be5\u6a21\u578b\u7c7b\u5173\u8054\u7684\u5176\u4ed6\u6a21\u578b\u7c7b\u7684\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cvalidates_associated :books<\/code> \u65b9\u6cd5\u7528\u4e8e\u9a8c\u8bc1\u4e0e Library<\/code> \u6a21\u578b\u7c7b\u5173\u8054\u7684\u6240\u6709\u4e66\u7c4d\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u5982\u679c\u4efb\u4f55\u4e00\u672c\u4e66\u7c4d\u7684\u6570\u636e\u65e0\u6548\uff0c\u90a3\u4e48\u6574\u4e2a\u56fe\u4e66\u9986\u5bf9\u8c61\u90fd\u65e0\u6cd5\u901a\u8fc7\u9a8c\u8bc1\uff0c\u5e76\u4e14\u4f1a\u5728\u6a21\u578b\u5bf9\u8c61\u7684 errors<\/code> \u96c6\u5408\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a books<\/code> \u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p> \n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cvalidates_associated<\/code> \u65b9\u6cd5\u53ea\u9a8c\u8bc1\u4e0e\u8be5\u6a21\u578b\u7c7b\u5173\u8054\u7684\u5176\u4ed6\u6a21\u578b\u7c7b\u7684\u6570\u636e\u662f\u5426\u6709\u6548\uff0c\u800c\u4e0d\u4f1a\u9a8c\u8bc1\u8be5\u6a21\u578b\u7c7b\u672c\u8eab\u7684\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u540c\u65f6\u9a8c\u8bc1\u8be5\u6a21\u578b\u7c7b\u672c\u8eab\u7684\u6570\u636e\u548c\u4e0e\u5176\u5173\u8054\u7684\u5176\u4ed6\u6a21\u578b\u7c7b\u7684\u6570\u636e\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u7684\u9a8c\u8bc1\u5668\u6765\u5b9e\u73b0\u3002<\/p> \n

confirmation\u786e\u8ba4<\/h3> \n
class Person < ApplicationRecord\n  validates :email, confirmation: true\nend\n<\/code><\/pre> \n 

\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a Rails \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 ActiveRecord \u6a21\u578b\u7c7b\uff0c\u5b83\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Person<\/code> \u7684\u7c7b\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684 email<\/code> \u5c5e\u6027\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p> \n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cvalidates<\/code> \u65b9\u6cd5\u7528\u4e8e\u4e3a email<\/code> \u5c5e\u6027\u6dfb\u52a0\u9a8c\u8bc1\u89c4\u5219\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528\u4e86 confirmation<\/code> \u9009\u9879\uff0c\u8868\u793a\u9700\u8981\u5bf9 email<\/code> \u5c5e\u6027\u8fdb\u884c\u786e\u8ba4\u9a8c\u8bc1\u3002\u8fd9\u610f\u5473\u7740\u5728\u8868\u5355\u4e2d\u63d0\u4ea4\u65f6\uff0c\u9664\u4e86\u8f93\u5165 email<\/code> \u5c5e\u6027\u7684\u503c\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u518d\u6b21\u8f93\u5165\u76f8\u540c\u7684\u503c\uff0c\u4ee5\u4fbf\u9a8c\u8bc1\u4e24\u6b21\u8f93\u5165\u7684\u503c\u662f\u5426\u4e00\u81f4\u3002\u5982\u679c\u4e24\u6b21\u8f93\u5165\u7684\u503c\u4e0d\u4e00\u81f4\uff0c\u5219\u4f1a\u5728\u6a21\u578b\u5bf9\u8c61\u7684 errors<\/code> \u96c6\u5408\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a email_confirmation<\/code> \u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p> \n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cconfirmation<\/code> \u9a8c\u8bc1\u5668\u53ea\u9002\u7528\u4e8e\u9700\u8981\u786e\u8ba4\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\u5bc6\u7801\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7b49\u3002\u5982\u679c\u8981\u5bf9\u5176\u4ed6\u7c7b\u578b\u7684\u5c5e\u6027\u8fdb\u884c\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7684\u9a8c\u8bc1\u5668\u6765\u5b9e\u73b0\u3002<\/p> \n

class Person < ApplicationRecord\n  validates :email, confirmation: true\n  validates :email_confirmation, presence: true\nend\n<\/code><\/pre> \n 

\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a Rails \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 ActiveRecord \u6a21\u578b\u7c7b\uff0c\u5b83\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Person<\/code> \u7684\u7c7b\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684 email<\/code> \u5c5e\u6027\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p> \n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cvalidates<\/code> \u65b9\u6cd5\u7528\u4e8e\u4e3a email<\/code> \u5c5e\u6027\u6dfb\u52a0\u9a8c\u8bc1\u89c4\u5219\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528\u4e86 confirmation<\/code> \u9009\u9879\uff0c\u8868\u793a\u9700\u8981\u5bf9 email<\/code> \u5c5e\u6027\u8fdb\u884c\u786e\u8ba4\u9a8c\u8bc1\u3002\u8fd9\u610f\u5473\u7740\u5728\u8868\u5355\u4e2d\u63d0\u4ea4\u65f6\uff0c\u9664\u4e86\u8f93\u5165 email<\/code> \u5c5e\u6027\u7684\u503c\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u518d\u6b21\u8f93\u5165\u76f8\u540c\u7684\u503c\uff0c\u4ee5\u4fbf\u9a8c\u8bc1\u4e24\u6b21\u8f93\u5165\u7684\u503c\u662f\u5426\u4e00\u81f4\u3002\u5982\u679c\u4e24\u6b21\u8f93\u5165\u7684\u503c\u4e0d\u4e00\u81f4\uff0c\u5219\u4f1a\u5728\u6a21\u578b\u5bf9\u8c61\u7684 errors<\/code> \u96c6\u5408\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a email_confirmation<\/code> \u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p> \n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fd8\u6dfb\u52a0\u4e86\u53e6\u4e00\u4e2a\u9a8c\u8bc1\u89c4\u5219\uff0c\u7528\u4e8e\u9a8c\u8bc1 email_confirmation<\/code> \u5c5e\u6027\u7684\u5b58\u5728\u6027\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u8fdb\u884c email<\/code> \u5c5e\u6027\u7684\u786e\u8ba4\u9a8c\u8bc1\u65f6\uff0c\u9700\u8981\u901a\u8fc7\u8868\u5355\u4e2d\u7684 email_confirmation<\/code> \u5c5e\u6027\u6765\u83b7\u53d6\u786e\u8ba4\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c email_confirmation<\/code> \u5c5e\u6027\u4e0d\u5b58\u5728\u6216\u4e3a\u7a7a\uff0c\u786e\u8ba4\u9a8c\u8bc1\u5c06\u65e0\u6cd5\u8fdb\u884c\uff0c\u56e0\u6b64\u9700\u8981\u9a8c\u8bc1\u5176\u5b58\u5728\u6027\u3002<\/p> \n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cconfirmation<\/code> \u9a8c\u8bc1\u5668\u53ea\u9002\u7528\u4e8e\u9700\u8981\u786e\u8ba4\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\u5bc6\u7801\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7b49\u3002\u5982\u679c\u8981\u5bf9\u5176\u4ed6\u7c7b\u578b\u7684\u5c5e\u6027\u8fdb\u884c\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7684\u9a8c\u8bc1\u5668\u6765\u5b9e\u73b0\u3002<\/p> \n

\u9a8c\u8bc1\u5927\u5c0f\u5199<\/strong><\/p> \n

class Person < ApplicationRecord\n  validates :email, confirmation: { case_sensitive: false }\nend\n<\/code><\/pre> \n 

\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a Rails \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 ActiveRecord \u6a21\u578b\u7c7b\uff0c\u5b83\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Person<\/code> \u7684\u7c7b\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684 email<\/code> \u5c5e\u6027\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p> \n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cvalidates<\/code> \u65b9\u6cd5\u7528\u4e8e\u4e3a email<\/code> \u5c5e\u6027\u6dfb\u52a0\u9a8c\u8bc1\u89c4\u5219\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528\u4e86 confirmation<\/code> \u9009\u9879\uff0c\u5e76\u4f20\u9012\u4e86\u4e00\u4e2a\u540d\u4e3a case_sensitive<\/code> \u7684\u54c8\u5e0c\u53c2\u6570\uff0c\u5c06\u5176\u8bbe\u7f6e\u4e3a false<\/code>\u3002\u8fd9\u610f\u5473\u7740\u5728\u8fdb\u884c\u7535","orderid":"0","title":"\u5173\u4e8eValidation\u7684\u65b9\u6cd5\u4f7f\u7528(\u4e00)","smalltitle":"","mid":"0","fname":"Ruby","special_id":"0","bak_id":"0","info":"0","hits":"424","pages":"5","comments":"0","posttime":"2023-07-23 13:24:53","list":"1690089893","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u5173\u4e8e<\/A> Validation<\/A> \u65b9\u6cd5\u4f7f<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"119.59.235.169","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5173\u4e8eValidation\u7684\u65b9\u6cd5\u4f7f\u7528","lastview":"1713310027","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}