{"id":12484,"date":"2020-12-26T23:02:30","date_gmt":"2020-12-26T14:02:30","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12484"},"modified":"2023-05-27T22:41:51","modified_gmt":"2023-05-27T13:41:51","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8c%e3%83%81%e3%82%a7%e3%83%83%e3%82%af%e3%83%9c%e3%83%83%e3%82%af%e3%82%b9%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=12484","title":{"rendered":"\u3010Ruby on Rails\u3011\u300c\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u300d\u3001\u300c\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u300d"},"content":{"rendered":"<h2>\u5358\u4e00\u306e\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9<\/h2>\n<h3>check_box_tag<\/h3>\n<p>\u5358\u4e00\u306e\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>label_tag<\/strong><\/span>\u3068\u5408\u308f\u305b\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= check_box_tag :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d %&gt;\r\n&lt;%= label_tag :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d, \"\u30e9\u30d9\u30eb\u5185\u5bb9\" %&gt;<\/pre>\n<h4>check_box_tag\u306e\u7528\u9014<\/h4>\n<p>\u30e2\u30c7\u30eb\u3068\u7d10\u3065\u3044\u3066\u3044\u306a\u3044<span style=\"color: #ff0000;\"><strong>form(form_tag)<\/strong><\/span>\u306e\u5834\u5408\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>checkbo_box<\/h3>\n<p>\u4e8b\u524d\u306b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u306bboolean\u578b\u3067checkbox\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">t.boolean :checkbox<\/pre>\n<p>view\u30d5\u30a1\u30a4\u30eb\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>check_box\u30e1\u30bd\u30c3\u30c9<\/strong><\/span>\u306b\u3066\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= form_with(model: \u30e2\u30c7\u30eb\u540d, local: true) do |form| %&gt;\r\n  &lt;div class=\"field\"&gt;\r\n    &lt;%= form.label :\u30e9\u30d9\u30eb\u540d%&gt;\r\n    &lt;%= form.check_box :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d, id: :id\u5c5e\u6027\u306e\u5024%&gt;\r\n  &lt;\/div&gt;\r\n \r\n  &lt;div class=\"actions\"&gt;\r\n    &lt;%= form.submit %&gt;\r\n  &lt;\/div&gt;\r\n\r\n&lt;% end %&gt;<\/pre>\n<p>DB\u306b\u306f0(false\uff1a\u30c1\u30a7\u30c3\u30af\u306a\u3057) or 1(true\uff1a\u30c1\u30a7\u30c3\u30af\u3042\u308a)\u304c\u767b\u9332\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4>check_box\u306e\u7528\u9014<\/h4>\n<p>\u30e2\u30c7\u30eb\u3068\u7d10\u3065\u3044\u3066\u3044\u308b<span style=\"color: #ff0000;\"><strong>form(form_with\u3001form_for)<\/strong><\/span>\u306e\u5834\u5408\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h4>\u521d\u671f\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/h4>\n<pre class=\"lang:default decode:true\">&lt;%= form.label :\u30e9\u30d9\u30eb\u5185\u5bb9 %&gt;\r\n&lt;%= form.check_box :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d, id: :id\u5c5e\u6027\u306e\u5024,checked:true %&gt;<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u300ccheck:true\u300d<\/strong><\/span>\u3067\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u8907\u6570\u9078\u629e\u306e\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9<\/h2>\n<h3>check_box_tag<\/h3>\n<h4>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u5074\u306e\u8a2d\u5b9a<\/h4>\n<p>text\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">t.text :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u683c\u7d0d\u5148\u306e\u30ab\u30e9\u30e0\u540d<\/pre>\n<h4>view\u5074\u306e\u8a2d\u5b9a<\/h4>\n<p>\u914d\u5217\u306b\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306b\u6e21\u3059\u6587\u5b57\u5217\u306e\u914d\u5217<\/strong><\/span>\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;% \u914d\u5217.each.with_index do |\u914d\u5217\u306e\u8981\u7d20,i| %&gt;\r\n  &lt;label&gt;\r\n    &lt;%= check_box_tag '\u30e2\u30c7\u30eb\u540d[\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u683c\u7d0d\u5148\u306e\u30ab\u30e9\u30e0\u540d][]', \u914d\u5217\u306e\u8981\u7d20,false,id:\"id_contents_#{i}\"%&gt;\r\n    &lt;%= \u30e9\u30d9\u30eb\u540d %&gt;\r\n  &lt;\/label&gt;\r\n&lt;% end %&gt;<\/pre>\n<h4>controller\u5074\u306e\u8a2d\u5b9a<\/h4>\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u914d\u5217\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">params.require(:\u30e2\u30c7\u30eb\u540d).permit(:name, :checkbox,\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u683c\u7d0d\u5148\u306e\u30ab\u30e9\u30e0\u540d:[])<\/pre>\n<h4>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u683c\u7d0d\u306e\u3055\u308c\u65b9<\/h4>\n<p>text\u578b\u306e\u30ab\u30e9\u30e0\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b\u914d\u5217\u306e\u5024\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">[\"\u9078\u629e\u50241\", \"\u9078\u629e\u50242\", \"\u9078\u629e\u50243\"]<\/pre>\n<h3>collection_check_boxes<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u9078\u629e\u80a2\u81ea\u4f53\u3092\u30c6\u30fc\u30d6\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u5834\u5408<\/strong><\/span>\u306fcollection_check_boxes\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u3070\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001<span style=\"color: #ff0000;\"><strong>\u753b\u9762\u304b\u3089<\/strong><strong>\u767b\u9332\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30c6\u30fc\u30d6\u30eb<\/strong><\/span>\u3068\u306f\u5225\u306b\u3001<span style=\"color: #ff0000;\"><strong>\u4f55\u3092\u9078\u3093\u3060\u304b\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb<\/strong><\/span>\u3001<span style=\"color: #ff0000;\"><strong>\u9078\u629e\u80a2\u81ea\u4f53\u3092\u4fdd\u5b58\u3059\u308b\u30c6\u30fc\u30d6\u30eb<\/strong><\/span>\u306e\u4e8c\u3064\u304c\u5fc5\u8981\u306b\u306a\u308b\u306e\u3067\u30c6\u30fc\u30d6\u30eb\u304c\u5897\u3048\u3059\u304e\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u70b9\u306b\u306f\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h4>\u767b\u9332\u5148\u30c6\u30fc\u30d6\u30eb\u306e\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a<\/h4>\n<pre class=\"lang:default decode:true\">class \u753b\u9762\u306e\u767b\u9332\u5148\u30c6\u30fc\u30d6\u30eb &lt; ActiveRecord::Base\r\n  has_many :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb(\u8907\u6570\u5f62)\r\n  has_many :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u5024\u4fdd\u5b58\u5148\u30c6\u30fc\u30d6\u30eb(\u8907\u6570\u5f62), :through =&gt; :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb(\u8907\u6570\u5f62)\r\nend<\/pre>\n<h4>\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u9078\u629e\u80a2\u306e\u4fdd\u5b58\u5148\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a<\/h4>\n<pre class=\"lang:default decode:true\">class \u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u9078\u629e\u80a2\u4fdd\u5b58\u5148\u30c6\u30fc\u30d6\u30eb &lt; ActiveRecord::Base\r\n  has_many :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb(\u8907\u6570\u5f62)\r\n  has_many :\u753b\u9762\u306e\u767b\u9332\u5148(\u8907\u6570\u5f62), :through =&gt; :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb(\u8907\u6570\u5f62)\r\nend<\/pre>\n<h4>\u30d3\u30e5\u30fc\u306e\u8a2d\u5b9a<\/h4>\n<pre class=\"lang:default decode:true\">&lt;div class=\"field\"&gt;\r\n  &lt;%= form.label :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb %&gt;&lt;br \/&gt;\r\n  &lt;%= collection_check_boxes(:\u753b\u9762\u306e\u767b\u9332\u5148\u30e2\u30c7\u30eb, :(\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb)_ids,\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb.all, :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb\u306eid(value\u306e\u5024), :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb\u306e\u30e9\u30d9\u30eb\u540d) do |b| %&gt;\r\n    &lt;%= b.label { b.check_box + b.text } %&gt;\r\n  &lt;% end %&gt;\r\n&lt;\/div&gt;<\/pre>\n<h5>\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb)_ids\uff1acollection_check_boxes\u306e\u7b2c\u4e8c\u5f15\u6570<\/h5>\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u9078\u3070\u308c\u305f\u9078\u629e\u80a2\u3092\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306b\u914d\u5217\u3068\u3057\u3066\u6e21\u3059\u305f\u3081\u306e\u8a2d\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h5>&lt;%= form.label :\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb %&gt;&lt;br \/&gt;<\/h5>\n<p>\u5225\u306b\u306a\u304f\u3066\u3082\u69cb\u3044\u307e\u305b\u3093\u304c\u3001\u3042\u308c\u3070\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u4fdd\u5b58\u5148\u3067\u3042\u308b\u3053\u3068\u3092\u30e9\u30d9\u30eb\u8868\u793a\u3057\u308c\u304f\u308c\u307e\u3059\u3002<\/p>\n<h5>&lt;%= b.label { b.check_box + b.text } %&gt;<\/h5>\n<p>b.label\u3067\u56f2\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u300c&lt;label for=\"\u5bfe\u5fdc\u3059\u308bid\"&gt;\u5404\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9&lt;\/label&gt;\u300d\u3068\u3044\u3046HTML\u304c\u51fa\u529b\u3055\u308c\u308b\u306e\u3067<span style=\"color: #ff0000;\"><strong>\u30e9\u30d9\u30eb\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3089\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u3092\u3064\u3051\u308b\u3068\u3044\u3046\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h4>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5074\u306e\u8a2d\u5b9a<\/h4>\n<pre class=\"lang:default decode:true \">def \u753b\u9762\u306e\u767b\u9332\u5148\u30e2\u30c7\u30eb_params\r\n  params.require(:\u753b\u9762\u306e\u767b\u9332\u5148\u30e2\u30c7\u30eb).permit(:name,\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb_ids:[])\r\nend<\/pre>\n<p>\u753b\u9762\u304b\u3089\u306f\u300c\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u9078\u629e\u80a2\u30e2\u30c7\u30eb_ids\u300d\u3068\u3044\u3046\u914d\u5217\u304c\u6e21\u308b\u306e\u3067Strong Parameter\u306e\u8a2d\u5b9a\u3067\u8a31\u53ef\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h2>\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3<\/h2>\n<h3>check_box<\/h3>\n<h4>\u9078\u629e\u80a2\u306e\u5fc5\u9808\u30c1\u30a7\u30c3\u30af<\/h4>\n<pre class=\"lang:default decode:true \">&lt;%= form.check_box :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d, id: :id\u540d,checked:true %&gt;<\/pre>\n<h4>\u30e2\u30c7\u30eb\u5074<\/h4>\n<pre class=\"lang:default decode:true \">validates :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d, presence: true<\/pre>\n<p>\u691c\u7d22\u3059\u308b\u3068<span style=\"color: #ff0000;\"><strong>\u300c<span class=\"n\">validates<\/span> <span class=\"ss\">:admin<\/span><span class=\"p\">,<\/span> <span class=\"ss\">inclusion: <\/span><span class=\"p\">{<\/span><span class=\"ss\">in: <\/span><span class=\"p\">[<\/span><span class=\"kp\">true<\/span><span class=\"p\">,<\/span> <span class=\"kp\">false<\/span><\/strong><\/span><span class=\"p\"><span style=\"color: #ff0000;\"><strong>]}\u300d<\/strong><\/span>\u306b\u3057\u306a\u3044\u3068false\u306e\u6642\u306b\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3068\u51fa\u3066\u304d\u307e\u3059\u304c\u3001Rails6\u306b\u306a\u3063\u305f\u304b\u3089\u3067\u3057\u3087\u3046\u304b\u3053\u308c\u3058\u3083\u306a\u3044\u3068\u3080\u3057\u308d\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/span><\/p>\n<h3>collection_check_boxes<\/h3>\n<h4>\u9078\u629e\u80a2\u306e\u5fc5\u9808\u30c1\u30a7\u30c3\u30af<\/h4>\n<pre class=\"lang:default decode:true\">&lt;%= collection_check_boxes(:\u753b\u9762\u3067\u767b\u9332\u3059\u308b\u30e2\u30c7\u30eb\u540d, :\u9078\u629e\u80a2\u30e2\u30c7\u30eb_ids, \u9078\u629e\u80a2\u30e2\u30c7\u30eb.all, :id, :name) do |b| %&gt;\r\n  &lt;%= b.label { b.check_box + b.text } %&gt;\r\n&lt;% end %&gt;<\/pre>\n<h4>\u30e2\u30c7\u30eb\u5074<\/h4>\n<pre class=\"lang:default decode:true  \">validates :\u9078\u629e\u30e2\u30c7\u30eb_ids, presence: true<\/pre>\n","protected":false},"excerpt":{"rendered":"\u5358\u4e00\u306e\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9 check_box_tag \u5358\u4e00\u306e\u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002label_tag\u3068\u5408\u308f\u305b\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002 &lt;%= check_box_tag :\u30d1\u30e9\u30e1\u30fc\u30bf\u540d %&gt; &lt; [&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\/12484"}],"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=12484"}],"version-history":[{"count":12,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12484\/revisions"}],"predecessor-version":[{"id":20491,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12484\/revisions\/20491"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12484"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}