{"id":12199,"date":"2020-11-16T11:42:20","date_gmt":"2020-11-16T02:42:20","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12199"},"modified":"2023-05-27T22:45:43","modified_gmt":"2023-05-27T13:45:43","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8ccocoon%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=12199","title":{"rendered":"\u3010Ruby on Rails\u3011\u300ccocoon\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>cocoon\u3068\u306f\uff1f<\/h2>\n<p>\u89aa\u5b50\u5b6b\u95a2\u4fc2\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u8907\u6570\u306e\u30c7\u30fc\u30bf\u3092\u540c\u6642\u306b\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3067\u304d\u308b\u306b\u3059\u308b\u305f\u3081\u306e\u30d3\u30e5\u30fc\u306b\u62e1\u5f35\u6027\u3092\u6301\u305f\u305b\u308b\u305f\u3081\u306eGem\u3067\u3059\u3002<\/p>\n<h2>\u30e2\u30c7\u30eb\u306e\u6307\u5b9a(cocoon\u3068\u306f\u76f4\u63a5\u95a2\u4fc2\u306a\u3044)<\/h2>\n<pre class=\"lang:default decode:true\">class \u89aa\u30e2\u30c7\u30eb &lt; ApplicationRecord\r\n  has_many :\u5b50\u30e2\u30c7\u30eb, dependent: :destroy\r\n  accepts_nested_attributes_for :\u5b50\u30e2\u30c7\u30eb, allow_destroy: true\r\nend<\/pre>\n<h3>accepts_nested_attributes_for<\/h3>\n<p>Rails\u304c\u6a19\u6e96\u3067\u63d0\u4f9b\u3057\u3066\u3044\u308bActiveRecord\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002cocoon\u3068\u306f\u76f4\u63a5\u95a2\u4fc2\u306a\u3044\u3067\u3059\u304c\u4e00\u7dd2\u306b\u4f7f\u308f\u308c\u308b\u30b1\u30fc\u30b9\u304c\u591a\u3044\u3067\u3059\u3002\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u89aa\u30e2\u30c7\u30eb\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306eparams\u306b<span style=\"color: #ff0000;\"><strong>\u300c{\u5b50\u30e2\u30c7\u30eb}_attributes\u300d<\/strong><\/span>\u3068\u3044\u3046\u5f62\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5177\u4f53\u7684\u306b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u5165\u308c\u5b50\u306b\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">        params.require(:\u89aa\u30e2\u30c7\u30eb).permit(:\u89aa\u306e\u30ab\u30e9\u30e0\uff11,\u5b50\u30e2\u30c7\u30eb_attributes: [:id,:\u5b50\u306e\u30ab\u30e9\u30e02,:_destroy])<\/pre>\n<h2>\u4e00\u3064\u306e\u30d3\u30e5\u30fc\u3067\u8907\u6570\u306e\u30e2\u30c7\u30eb\u3092\u4f7f\u3046\u65b9\u6cd5<\/h2>\n<h3>fields_for<\/h3>\n<p>\u3053\u308c\u3082\u3001cocoon\u3068\u306f\u95a2\u4fc2\u306a\u3044\u3067\u3059\u304c\u89aa\u30e2\u30c7\u30eb\u306b\u95a2\u9023\u3059\u308b\u5b50\u30e2\u30c7\u30eb\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f<span style=\"color: #ff0000;\"><strong>fields_for<\/strong><\/span>\u3092\u4f7f\u3046\u3068\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">&lt;%= form_with(model: @\u89aa\u30e2\u30c7\u30eb, local: true) do |s| %&gt;\r\n    &lt;%= s.fields_for :\u5b50\u30e2\u30c7\u30eb do |t| %&gt;\r\n    &lt;% end %&gt;\r\n&lt;% end %&gt;<\/pre>\n<h2>cocoon\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9<\/h2>\n<div>\n<h3>link_to_add_association<\/h3>\n<p>\u5b50\u8981\u7d20\u3092\u4e00\u3064\u8ffd\u52a0\u3059\u308b\u305f\u3081\u306e\u30ea\u30f3\u30af\u3092view\u306b\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= link_to_add_association \"\u30e9\u30d9\u30eb\", \u89aa\u30d5\u30a9\u30fc\u30e0, :\u5b50\u30e2\u30c7\u30eb,data:{association_insertion_node:'\u8ffd\u52a0\u5bfe\u8c61\u306eclass',association_insertion_method:'\u30e1\u30bd\u30c3\u30c9'} %&gt;<\/pre>\n<p>\u306a\u304a\u3001<span style=\"color: #ff0000;\"><strong>\u300cassociation_insertion_node\u300d<\/strong><\/span>\u3084<span style=\"color: #ff0000;\"><strong>\u300cassociation_insertion_method\u300d<\/strong><\/span>\u3092\u4f7f\u3046\u3053\u3068\u3067\u3069\u3053\u306b\u5b50\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u304b\u3092\u7d30\u304b\u304f\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<h3>link_to_remove_association<\/h3>\n<p>\u30e2\u30c7\u30eb\u3092\u524a\u9664\u3059\u308b\u305f\u3081\u306e\u30ea\u30f3\u30af\u3092view\u306b\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002<\/p>\n<div>\n<div>\n<pre class=\"lang:default decode:true\">&lt;%= link_to_remove_association \"\u30e9\u30d9\u30eb\", \u524a\u9664\u5bfe\u8c61\u306e\u30e2\u30c7\u30eb %&gt;<\/pre>\n<p>\u306a\u304a\u3001\u4e0a\u8a18\u30e1\u30bd\u30c3\u30c9\u3092<span style=\"color: #ff0000;\"><strong>Rails6\u4ee5\u964d<\/strong><\/span>\u3067\u52d5\u4f5c\u3055\u305b\u3088\u3046\u3068\u3057\u305f\u5834\u5408\u306f\u4e0b\u8a18\u306e\u624b\u9806\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u6ce8\u610f\u3002<\/p>\n<p>https:\/\/qiita.com\/ashketcham\/items\/87e3db665ca9c66ce673<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"cocoon\u3068\u306f\uff1f \u89aa\u5b50\u5b6b\u95a2\u4fc2\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u8907\u6570\u306e\u30c7\u30fc\u30bf\u3092\u540c\u6642\u306b\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3067\u304d\u308b\u306b\u3059\u308b\u305f\u3081\u306e\u30d3\u30e5\u30fc\u306b\u62e1\u5f35\u6027\u3092\u6301\u305f\u305b\u308b\u305f\u3081\u306eGem\u3067\u3059\u3002 \u30e2\u30c7\u30eb\u306e\u6307\u5b9a(cocoon\u3068\u306f\u76f4\u63a5\u95a2\u4fc2\u306a\u3044) class \u89aa\u30e2\u30c7\u30eb &lt; A [&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\/12199"}],"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=12199"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12199\/revisions"}],"predecessor-version":[{"id":20505,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12199\/revisions\/20505"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12199"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}