{"id":7540,"date":"2019-11-26T21:16:08","date_gmt":"2019-11-26T12:16:08","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7540"},"modified":"2023-05-27T22:59:30","modified_gmt":"2023-05-27T13:59:30","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%83%90%e3%83%aa%e3%83%87%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e5%87%a6%e7%90%86%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7540","title":{"rendered":"\u3010Ruby on Rails\u3011\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u51e6\u7406\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u65e5\u672c\u8a9e\u5316"},"content":{"rendered":"<h2>validates\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>\u4e0b\u8a18\u306e\u69cb\u6587\u3092\u4f7f\u3046\u3053\u3068\u3067\u30e2\u30c7\u30eb\u306e\u30ab\u30e9\u30e0\u306b\u5bfe\u3057\u3066\u691c\u8a3c\u30eb\u30fc\u30eb\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">validates \u30ab\u30e9\u30e0\u540d,\u691c\u8a3c\u30eb\u30fc\u30eb<\/pre>\n<h3>app\/models\/\u30e2\u30c7\u30eb\u540d.rb<\/h3>\n<p>validates\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u969b\u306b\u8a18\u8ff0\u3059\u308b\u30bd\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306b\u306a\u308a\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4f55\u3082\u30af\u30e9\u30b9\u5185\u306b\u4f55\u3082\u51e6\u7406\u304c\u8a18\u8ff0\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">class User &lt; ApplicationRecord\r\nend<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7de8\u96c6\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">class User &lt; ApplicationRecord\r\n  validates :username,presence: true\r\nend<\/pre>\n<p>\u305d\u3046\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>\u672a\u5165\u529b\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7551\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/11\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-11-26-21.43.23.png\" alt=\"\" width=\"1116\" height=\"500\" srcset=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/11\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-11-26-21.43.23.png 1116w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/11\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-11-26-21.43.23-300x134.png 300w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/11\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-11-26-21.43.23-768x344.png 768w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/11\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-11-26-21.43.23-1024x459.png 1024w\" sizes=\"(max-width: 1116px) 100vw, 1116px\" \/><\/p>\n<h2>Rails\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u691c\u8a3c\u30eb\u30fc\u30eb<\/h2>\n<h3>presence<\/h3>\n<p>\u672a\u5165\u529b\u3092\u8a31\u53ef\u3057\u306a\u3044\u3002\uff08allow_blank\u306e\u9006\uff09<\/p>\n<h3>allow_blank<\/h3>\n<p>\u672a\u5165\u529b\u3092\u8a31\u53ef\u3059\u308b\u3002\uff08presence\u306e\u9006\uff09<\/p>\n<h3>numericality<\/h3>\n<p>\u6570\u5024\u306e\u307f\u8a31\u53ef\u3059\u308b\u3002<\/p>\n<h4>only_integer<\/h4>\n<pre class=\"lang:default decode:true \">numericality:{only_integer:true}<\/pre>\n<p>\u5c11\u6570\u3092\u8a31\u53ef\u3057\u306a\u3044\u3002numericality\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3068\u3057\u3066\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>format:{with:\u6b63\u898f\u8868\u73fe}<\/h3>\n<p>\u6b63\u898f\u8868\u73fe\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>inclusion<\/h3>\n<p>\u6570\u5024\u306e\u7bc4\u56f2\u3084\u3001\u6587\u5b57\u5217\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3084\u69cb\u6210\u6587\u5b57\u7a2e\u304c\u60f3\u5b9a\u3069\u304a\u308a\u304b\u3002<\/p>\n<h3>uniqueness<\/h3>\n<p>\u65e2\u306b\u540c\u4e00\u306e\u30c7\u30fc\u30bf\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<h4>case_sensitive<\/h4>\n<p>\u5927\u6587\u5b57\u3068\u5c0f\u6587\u5b57\u306e\u533a\u5225\u3092\u3057\u307e\u3059\u3002<\/p>\n<h3>length<\/h3>\n<p>\u9577\u3055\u5236\u9650\u3092\u3059\u308b\u3002<\/p>\n<h4>maximum<\/h4>\n<p>\u9577\u3055\u306e\u4e0a\u9650<\/p>\n<h4>minimum<\/h4>\n<p>\u9577\u3055\u306e\u4e0b\u9650<\/p>\n<h4>in: \u6700\u5c0f\u9577..\u6700\u5927\u9577<\/h4>\n<p>\u9577\u3055\u306e\u7bc4\u56f2<\/p>\n<h4>is: \u9577\u3055\u56fa\u5b9a\u6307\u5b9a<\/h4>\n<p>\u9577\u3055\u306e\u6307\u5b9a<\/p>\n<h3>confirmation<\/h3>\n<p>\u30d1\u30b9\u30ef\u30fc\u30c9\u3084\u30e1\u30a2\u30c9\u304c\u78ba\u8a8d\u7528\u306e\u5165\u529b\u5185\u5bb9\u3068\u4e00\u81f4\u3057\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<\/p>\n<h2>\u3069\u3053\u306b\u30a8\u30e9\u30fc\u304c\u683c\u7d0d\u3055\u308c\u308b\u304b\u3002<\/h2>\n<p>ActiveModel::Errors\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eerros\u306b\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u7b49\u304c\u683c\u7d0d\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.erros<\/strong><\/span>\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u72ec\u81ea\u306e\u691c\u8a3c\u30eb\u30fc\u30eb<\/h2>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u8a18\u8ff0\u3059\u308c\u3070<span style=\"color: #ff0000;\"><strong>\u72ec\u81ea\u306e\u691c\u8a3c\u30eb\u30fc\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">class User &lt; ApplicationRecord\r\n  validate :\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u540d\r\n\r\n  private\r\n    def \u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u540d\r\n      \u72ec\u81ea\u306e\u691c\u8a3c\u51e6\u7406\r\n    end\r\nend<\/pre>\n<p>\u72ec\u81ea\u306e\u691c\u8a3c\u30eb\u30fc\u30eb\u3092\u4f5c\u308a\u969b\u306f<span style=\"color: #ff0000;\"><strong>\u300cvalidates\u30e1\u30bd\u30c3\u30c9\u300d\u3067\u306f\u306a\u304f\u300cvalidate\u30e1\u30bd\u30c3\u30c9\u300d\u306b\u306a\u308b\u306e\u3067\u6ce8\u610f<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>errors.add\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u691c\u8a3c\u3067\u30a8\u30e9\u30fc\u306b\u306a\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306e\u69cb\u6587\u3067\u30a8\u30e9\u30fc\u3092\u753b\u9762\u306b\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">errors.add(\u30ab\u30e9\u30e0\u540d,'\u30e1\u30c3\u30bb\u30fc\u30b8')<\/pre>\n<h2>\u65e5\u672c\u8a9e\u5316<\/h2>\n<p>Rails\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306f<span style=\"color: #ff0000;\"><strong>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u82f1\u8a9e\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/strong><\/span>\u82f1\u8a9e\u4ee5\u5916\u306e\u8a00\u8a9e\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u306e\u624b\u9806\u3092\u5b9f\u65bd\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\uff08\u624b\u9806\u306f\u65e5\u672c\u8a9e\u306b\u7ffb\u8a33\u3059\u308b\u624b\u9806\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n<p>Gemfile\u306b\u4e0b\u8a18\u306eGem\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">gem 'rails-i18n'<\/pre>\n<p>Rails\u30b5\u30fc\u30d0\u30fc\u3092\u518d\u8d77\u52d5\u3059\u308c\u3070\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u3042\u308b\u7a0b\u5ea6\u65e5\u672c\u8a9e\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u500b\u5225\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u540d\u306f\u65e5\u672c\u8a9e\u5316\u3055\u308c\u306a\u3044\u306e\u3067<span style=\"color: #ff0000;\"><strong>config\/locales\/ja.yml<\/strong><\/span>\u306b\u3066\u65e5\u672c\u8a9e\u540d\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">ja:\r\n  activerecord:\r\n    attributes:\r\n      \u30e2\u30c7\u30eb\u540d:\r\n        name: \u65e5\u672c\u8a9e\u540d1\r\n        age: \u65e5\u672c\u8a9e\u540d2\u30d5<\/pre>\n<h3>rails-i18n\u30ea\u30dd\u30b8\u30c8\u30ea\u304b\u3089\u7ffb\u8a33\u30d5\u30a1\u30a4\u30eb\u3092\u53d6\u5f97\u3059\u308b\u3002<\/h3>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066(\u30a2\u30d7\u30ea\u540d)\/config\/locales\u306b\u65e5\u672c\u8a9e\u7ffb\u8a33\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">wget https:\/\/raw.github.com\/svenfuchs\/rails-i18n\/master\/rails\/locale\/ja.yml -P config\/locales\/<\/pre>\n<p>\u81ea\u8eab\u3067\u4f5c\u6210\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u304c\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306bGitHub\u306e<span style=\"color: #ff0000;\"><strong>rails-i18n\u30ea\u30dd\u30b8\u30c8\u30ea<\/strong><\/span>\u306b\u65e2\u306b\u7ffb\u8a33\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u308b\u306e\u3067\u305d\u308c\u3092\u5229\u7528\u3059\u308b\u304c\u65e9\u3044\u3067\u3059\u3002<\/p>\n<p>\u300c(\u30a2\u30d7\u30ea\u540d)\/config\/initializers\/locale.rb\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u65b0\u305f\u306b\u4f5c\u6210\u3057\u3066\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">Rails.application.config.i18n.default_locale = :ja<\/pre>\n<p>\u3053\u306e\u8a2d\u5b9a\u3092\u3059\u308c\u3070\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u65e5\u672c\u8a9e\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u4f7f\u3046\u3088\u3046\u306b\u8a2d\u5b9a\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u3082\u3057\u304f\u306f\u3001\u300c(\u30a2\u30d7\u30ea\u540d)\/config\/application.rb\u300d\u306b\u4ee5\u4e0b\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3059\u308b\u306e\u3082\u3042\u308a\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">config.i18n.default_locale = :ja<\/pre>\n","protected":false},"excerpt":{"rendered":"validates\u30e1\u30bd\u30c3\u30c9 \u4e0b\u8a18\u306e\u69cb\u6587\u3092\u4f7f\u3046\u3053\u3068\u3067\u30e2\u30c7\u30eb\u306e\u30ab\u30e9\u30e0\u306b\u5bfe\u3057\u3066\u691c\u8a3c\u30eb\u30fc\u30eb\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002 validates \u30ab\u30e9\u30e0\u540d,\u691c\u8a3c\u30eb\u30fc\u30eb app\/models\/\u30e2\u30c7\u30eb\u540d.rb validates\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u969b\u306b [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7540"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7540"}],"version-history":[{"count":13,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7540\/revisions"}],"predecessor-version":[{"id":20543,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7540\/revisions\/20543"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7540"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}