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

 <\/p> \n

Active Record \u9a8c\u8bc1\u8f85\u52a9\u65b9\u6cd5\u7684\u9ed8\u8ba4\u9519\u8bef\u6d88\u606f\u90fd\u662f\u82f1\u6587\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u6709\u65f6\u5019\u6211\u4eec\u7ecf\u5e38\u4f1a\u88ab\u8981\u6c42\u6309\u7279\u5b9a\u7684\u6587\u672c\u5c55\u793a\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u65f6\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p> \n

1. \u76f4\u63a5\u5728:message\u6dfb\u52a0\u6587\u6848<\/p> \n

\n
class User < ActiveRecord::Base
\u3000validates :email,
<\/span> presence: { message: <\/span>'<\/span>\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a\uff01<\/span>'<\/span> },
<\/span> uniqueness: { message: <\/span>'\u90ae\u7bb1 %{value} <\/span>\u5df2\u5b58\u5728\uff01<\/span>'<\/span> }
<\/span> validates :name,
<\/span> presence: { message: <\/span>'<\/span>\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01<\/span>'<\/span> },
<\/span><\/em><\/em><\/em><\/em> length: { maximum: <\/span>255, too_long: '<\/span>\u59d3\u540d\u6700\u591a\u4e3a255\u4e2a\u5b57\u7b26<\/span>'<\/span> }
<\/span><\/em><\/em><\/em><\/em><\/em><\/em>end<\/em><\/em><\/em><\/em><\/em><\/em><\/em><\/pre> \n <\/div> \n

\u9a8c\u8bc1\u4e00\u4e0b\uff1a<\/p> \n

\n
user = User.new\r\nuser.valid?\r\nuser.errors.messages\r\n<\/span>#<\/span>=> { :email=>["\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a\uff01"], :name=>["\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01"]} <\/span><\/pre> \n <\/div> \n 

2. \u4f7f\u7528 rails\u56fd\u9645\u5316API<\/p> \n

\u5728config\/locales\u6587\u4ef6\u4e0b\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u9ed8\u8ba4\u7684en.yml\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u518d\u6dfb\u52a0\u4e00\u4e2a zh-CN.yml \u6587\u4ef6\u4f5c\u4e3a\u4e2d\u6587\u7684\u7ffb\u8bd1<\/p> \n

\n
class<\/span> ApplicationController < ActionController::Base<\/span>\r\n  before_action :set_locale\r\n  <\/span>def<\/span> set_locale\r\n     I18n.locale <\/span>= user_locale\r\n    <\/span>#<\/span> after store current locale<\/span>\r\n    cookies[:locale] = params[:locale] if<\/span> params[:locale]\r\n  rescue I18n::InvalidLocale\r\n    I18n.locale <\/span>= I18n.default_locale\r\n  end\r\n\r\n  protected\r\n  <\/span>def<\/span> user_locale\r\n    params[:locale] <\/span>|| cookies[:locale] || http_head_locale || I18n.default_locale\r\n  end
<\/span><\/pre> \n
  def http_head_locale
request.env['HTTP_ACCEPT_LANGUAGE']
end<\/pre> \n
end<\/span><\/pre> \n <\/div> \n 

\u8bbe\u7f6e\u6bcf\u4e2akey\u5bf9\u5e94\u7684\u7ffb\u8bd1\u503c<\/p> \n

\n
zh-CN:\r\n  activerecord:\r\n    attributes:\r\n      taken: <\/span>'<\/span>\u5df2\u5360\u7528<\/span>'<\/span>\r\n      blank: <\/span>'<\/span>\u4e0d\u80fd\u4e3a\u7a7a<\/span>'<\/span>\r\n      user:\r\n        email: <\/span>'<\/span>\u90ae\u7bb1<\/span>'<\/span>\r\n        password: <\/span>'<\/span>\u5bc6\u7801<\/span>'<\/span>\r\n        name: <\/span>'<\/span>\u59d3\u540d<\/span>'<\/span>\r\n        mobile: <\/span>'<\/span>\u624b\u673a\u53f7<\/span>'<\/span>\r\n    models:\r\n      user: <\/span>'<\/span>\u7528\u6237<\/span>'<\/span>\r\n    errors:\r\n      messages:\r\n        record_invalid: <\/span>"<\/span>%{errors}<\/span>"<\/span>\r\n        taken: <\/span>'<\/span>\u5df2\u5360\u7528<\/span>'<\/span>\r\n        blank: <\/span>'<\/span>\u4e0d\u80fd\u4e3a\u7a7a<\/span>'<\/span>\r\n      attributes:\r\n        version: <\/span>'<\/span>\u7248\u672c<\/span>'<\/span>\r\n  actions:\r\n    create: <\/span>'<\/span>\u65b0\u589e<\/span>'<\/span>\r\n    update: <\/span>'<\/span>\u4fee\u6539<\/span>'<\/span>\r\n    destroy: <\/span>'<\/span>\u5220\u9664<\/span>'<\/span><\/pre> \n <\/div> \n 

\u9a8c\u8bc1\u4e00\u4e0b\uff1a<\/p> \n

\n
class<\/span> User < ActiveRecord::Base\r\n  validates :mobile,\r\n            presence:   true,\r\n            uniqueness: true,\r\nend<\/span><\/pre> \n <\/div> \n 
\n
user = User.new\r\nuser.valid?\r\nuser.errors.messages  <\/span>#<\/span>=> { :mobile=>["\u4e0d\u80fd\u4e3a\u7a7a", "\u5df2\u5360\u7528"] } <\/span>\r\nuser.errors.full_messages  #<\/span>=> [ "\u624b\u673a\u53f7 \u4e0d\u80fd\u4e3a\u7a7a", "\u624b\u673a\u53f7 \u5df2\u5360\u7528"]<\/span><\/pre> \n <\/div> \n 

\u53c2\u8003\uff1a<\/p> \n

\u5904\u7406\u9a8c\u8bc1\u9519\u8bef<\/a><\/p> \n

Active Record \u6a21\u578b\u7684\u7ffb\u8bd1<\/a><\/p> \n

 <\/p> \n<\/div>","orderid":"0","title":"Rails \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u7684\u9519\u8bef\u4fe1\u606f","smalltitle":"","mid":"0","fname":"Ruby","special_id":"0","bak_id":"0","info":"0","hits":"374","pages":"1","comments":"0","posttime":"2019-09-03 03:38:15","list":"1567453095","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":"Rails<\/A> \u5b9a\u4e49<\/A> \u9a8c\u8bc1<\/A> \u9519\u8bef<\/A> \u4fe1\u606f<\/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":"120.229.33.54","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Rails \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u7684\u9519\u8bef\u4fe1\u606f","lastview":"1713291113","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}