{"id":1408,"date":"2013-01-07T12:54:46","date_gmt":"2013-01-07T03:54:46","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1408"},"modified":"2018-11-14T16:33:57","modified_gmt":"2018-11-14T07:33:57","slug":"rails%e3%81%a7%e7%8b%ac%e8%87%aa%e3%81%ae%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%93%e3%83%ab%e3%83%80%e3%83%bc%e3%82%92%e4%bd%9c%e3%82%8d%e3%81%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/01\/07\/rails%e3%81%a7%e7%8b%ac%e8%87%aa%e3%81%ae%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%93%e3%83%ab%e3%83%80%e3%83%bc%e3%82%92%e4%bd%9c%e3%82%8d%e3%81%86%ef%bc%81\/","title":{"rendered":"Rails\u3067\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u308d\u3046\uff01"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails \u3067\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u308b\u3068\u4fbf\u5229\u306a\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3068\u306f<\/h2>\n<p>Rails \u3067\u30d5\u30a9\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306b form_for \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001form_for \u306b\u6e21\u3059\u30d6\u30ed\u30c3\u30af\u306e\u5f15\u6570\u306b\u6e21\u3055\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3067\u3059\u3002<\/p>\n<p>\u8a00\u8449\u3067\u306f\u5206\u304b\u308a\u3065\u3089\u3044\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u304a\u3051\u308b\u300cform\u300d\u304c\u305d\u308c\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= form_for(@user) do |form| %&gt;\r\n  # \u30d6\u30ed\u30c3\u30af\u5f15\u6570\u306e form \u304c\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3067\u3059.\r\n&lt;% end %&gt;<\/pre>\n<p>\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u306e\u5b9f\u4f53\u306f ActionView::Helpers::FormBuilder \u30af\u30e9\u30b9\u3067\u3042\u308a\u3001\u30d6\u30ed\u30c3\u30af\u5f15\u6570\u306b\u6e21\u3055\u308c\u308b\u306e\u306f ActionView::Helpers::FormBuilder \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3059\u3002<\/p>\n<p>\u666e\u6bb5\u300c&lt;%= form.text_field(:name) %&gt;\u300d\u306e\u3088\u3046\u306b\u4fbf\u5229\u306b\u4f7f\u3063\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u306f ActionView::Helpers::FormBuilder \u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>error_message_on \u3092\u66f8\u304f\u306e\u304c\u9762\u5012<\/h2>\n<p>\u3068\u304d\u3069\u304d\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u300cform.text_field(...)\u300d\u306e\u3088\u3046\u306b\u5165\u529b\u9805\u76ee\u3092\u4f5c\u308a\u3001\u3059\u3050\u5f8c\u306b\u300cform.error_message_on(...)\u300d\u3067\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3059\u308b\u3001\u3068\u3044\u3046\u30b3\u30fc\u30c9\u3092\u4f55\u5ea6\u3082\u66f8\u304f\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= form_for(@user) do |form| %&gt;\r\n  \u540d\u524d\uff1a\r\n  &lt;%= form.text_field(:name) %&gt;\r\n  &lt;%= form.error_message_on(:name) %&gt;\r\n\r\n  \u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\uff1a\r\n  &lt;%= form.text_field(:email) %&gt;\r\n  &lt;%= form.error_message_on(:email) %&gt;\r\n\r\n  ...\r\n&lt;% end %&gt;<\/pre>\n<p>\u6570\u304c\u5c11\u306a\u3044\u5834\u5408\u306f\u826f\u3044\u3067\u3059\u304c\u3001\u5165\u529b\u9805\u76ee\u306e\u591a\u3044\u30d5\u30a9\u30fc\u30e0\u3060\u3068\u300cform.error_message_on(...)\u300d\u3092\u66f8\u304f\u3053\u3068\u304c\u9762\u5012\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u3088\u3046\u306a\u3068\u304d\u306f\u3001\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u6210\u3057\u3001\u81ea\u52d5\u7684\u306b\u300cform.error_message_on(...)\u300d\u3092\u633f\u5165\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3068\u697d\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u203b\u88dc\u8db3: Rails3 \u304b\u3089\u306f error_message_on \u3084 error_message_for \u306f dynamic_form \u3068\u3044\u3046 Gem \u306b\u5916\u51fa\u3057\u3055\u308c\u307e\u3057\u305f\u306e\u3067\u3001\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f Gemfile \u306b dynamic_form \u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u308b<\/h2>\n<p>\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u308b\u306b\u306f\u3001ActionView::Helpers::FormBuilder \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a text_field \u306b\u81ea\u52d5\u7684\u306b error_message_on \u3092\u8ffd\u52a0\u3059\u308b\u3088\u3046\u306b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class CustomFormBuilder &lt; ActionView::Helpers::FormBuilder\r\n\r\n  def text_field(attribute, options={})\r\n    super + error_message_on(attribute)\r\n  end \r\n\r\nend<\/pre>\n<p>\u300csuper\u300d\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u51e6\u7406\u3092\u547c\u3073\u51fa\u3057\u3001\u305d\u306e\u7d50\u679c\u306b\u300cerror_message_on(attribute)\u300d\u306e\u7d50\u679c\u3092\u52a0\u3048\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f5c\u6210\u3057\u305f CustomFormBuilder \u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b form_for \u306e\u30aa\u30d7\u30b7\u30e7\u30f3 :builder \u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&lt;%= form_for(@user, :builder =&gt; CustomFormBuilder) do |form| %&gt;\r\n  \u540d\u524d\uff1a\r\n  &lt;%= form.text_field(:name) %&gt;\r\n\r\n  \u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\uff1a\r\n  &lt;%= form.text_field(:email) %&gt;\r\n  ...\r\n&lt;% end %&gt;<\/pre>\n<p>text_field \u306e\u5f8c\u306b\u306f\u81ea\u52d5\u7684\u306b error_message_on \u304c\u633f\u5165\u3055\u308c\u308b\u306e\u3067\u3001\u30b3\u30fc\u30c9\u304b\u3089 error_message_on \u306f\u6d88\u3048\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u306f text_field \u30e1\u30bd\u30c3\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u307e\u3057\u305f\u304c\u3001\u5b9f\u7528\u7684\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u4ed5\u4e0a\u3052\u308b\u306b\u306f text_area \u3084 collection_select \u306a\u3069\u4ed6\u306e\u30e1\u30bd\u30c3\u30c9\u3082\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3067\u306f\u306a\u304f\u65b0\u3057\u3044\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3001\u3068\u3044\u3046\u3088\u3046\u306b\u5b9f\u73fe\u306e\u4ed5\u65b9\u306f\u4e00\u901a\u308a\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3054\u3068\u306b\u30d9\u30b9\u30c8\u306a\u3084\u308a\u65b9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff01<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails \u3067\u72ec\u81ea\u306e\u30d5\u30a9\u30fc\u30e0\u30d3\u30eb\u30c0\u30fc\u3092\u4f5c\u308b\u3068\u4fbf\u5229\u306a\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/01\/07\/rails%e3%81%a7%e7%8b%ac%e8%87%aa%e3%81%ae%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%93%e3%83%ab%e3%83%80%e3%83%bc%e3%82%92%e4%bd%9c%e3%82%8d%e3%81%86%ef%bc%81\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1408"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"predecessor-version":[{"id":17985,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions\/17985"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}