{"id":7836,"date":"2019-12-02T23:58:15","date_gmt":"2019-12-02T14:58:15","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7836"},"modified":"2023-05-27T22:55:35","modified_gmt":"2023-05-27T13:55:35","slug":"%e3%80%90ruby-on-rails%e3%80%91rails%e3%81%a7%e3%81%aeajax%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7836","title":{"rendered":"\u3010Ruby on Rails\u3011Rails\u3067\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8(rails-ujs\u3001SJR\u306a\u3069)"},"content":{"rendered":"<p>\u672c\u8a18\u4e8b\u3067\u306fRails\u3067\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u3064\u3044\u3066\u306e\u89e3\u8aac\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<h2>Rails\u3067\u306eAjax\u306e\u5b9f\u88c5<\/h2>\n<h3>\u30d3\u30e5\u30fc\u5074<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>link_to\u30e1\u30bd\u30c3\u30c9<\/strong><\/span>\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">remote: true<\/pre>\n<p>\u3053\u308c\u3060\u3051\u3067\u30ea\u30f3\u30af\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3060\u3051\u3067Ajax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30b5\u30fc\u30d0\u306b\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5074<\/h3>\n<p>\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">head :no_content<\/pre>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u30dc\u30c7\u30a3\u306a\u3057\u3067HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u3092204\u3067\u8fd4\u3059\u3068\u3044\u3046\u8a18\u8ff0\u306b\u306a\u308a\u307e\u3059\u3002\uff08\u5225\u306b\u8a18\u8ff0\u306f\u3076\u3063\u3061\u3083\u3051\u306a\u304f\u3066\u3082\u52d5\u304d\u307e\u3059\u304c\u660e\u793a\u7684\u306b\u66f8\u3044\u3066\u3044\u307e\u3059\u3002\uff09<\/p>\n<h3>JavaScript<\/h3>\n<p>JavaScript\u5074\u3067\u30b5\u30fc\u30d0\u5074\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8\u3067\u4f9d\u983c\u3057\u305f\u51e6\u7406\u7d50\u679c\u3092\u753b\u9762\u306b\u53cd\u6620\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4e0b\u8a18\u306fturbolinks\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306e\u8a18\u8ff0\u3067\u3059\u304c\u5229\u7528\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u306fwindows.onload\u306b\u66f8\u304d\u63db\u3048\u3066\u4e0b\u3055\u3044\u3002<\/p>\n<pre class=\"lang:default decode:true\"> document.addEventListener('turbolinks:load', function() {\r\n   document.querySelectorAll('\u524a\u9664\u30dc\u30bf\u30f3\u306e\u30af\u30e9\u30b9').forEach(function(HTML\u8981\u7d20\u540d) {\r\n     HTML\u8981\u7d20\u540d.addEventListener('ajax:success', function() {\r\n\u3000\u3000\u3000\u3000\u884c\u3044\u305f\u3044\u51e6\u7406\u5185\u5bb9\r\n     });\r\n   });\r\n });<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Ajax\u901a\u4fe1\u304c\u6210\u529f\u3057\u305f\u969b\u306fRails\u304cajax:success\u3068\u3044\u3046\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u884c\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3053\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002Ajax\u901a\u4fe1\u304c\u6210\u529f\u3057\u305f\u3089\u3053\u306e\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\u304c\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u975e\u540c\u671f\u306b\u884c\u3044\u305f\u3044\u51e6\u7406\u3092\u8a18\u8ff0\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>Rails\u3067Ajax\u901a\u4fe1\u3092\u5b9f\u73fe\u3057\u3066\u3044\u308b\u4ed5\u7d44\u307f(rails-ujs)<\/h2>\n<p>link_to\u306b\u300cremote: true\u300d\u3068\u3044\u3046\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u52a0\u3048\u308b\u3068Ajax\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u4ed5\u7d44\u307f\u306f<span style=\"color: #ff0000;\"><strong>\u300crails-ujs\u300d<\/strong><\/span>\u3068\u3044\u3046ActionView\u306eJavaScript\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3088\u3063\u3066\u51e6\u7406\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u307e\u305fAjax\u901a\u4fe1\u304c\u6210\u529f\u3057\u305f\u5834\u5408\uff08\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u304c2XX\u306e\u5834\u5408\uff09\u306f<span style=\"color: #ff0000;\"><strong>\u300cajax:success\u300d\u30a4\u30d9\u30f3\u30c8<\/strong><\/span>\u3092\u767a\u884c\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u753b\u9762\u5074\u3067\u306f\u4efb\u610f\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u753b\u9762\u66f4\u65b0\u7b49\u306e\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001rails-ujs\u306fRails5.1\u304b\u3089\u3067\u305d\u308c\u307e\u3067\u306fjquery-rails\u3068\u3044\u3046JavaScript\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u4f7f\u308f\u308c\u3066\u3044\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<h3>\u5931\u6557\u3057\u305f\u5834\u5408\u306f<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u300cajax:error\u300d\u30a4\u30d9\u30f3\u30c8<\/strong><\/span>\u304c\u51e6\u7406\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>\u305d\u306e\u4ed6\u306erails-ujs\u306e\u6a5f\u80fd(Ajax\u4ee5\u5916\u306b\u3082\u69d8\u3005\u306a\u753b\u9762\u6a5f\u80fd\u304c\u3042\u308b\u3002\uff09<\/h3>\n<h4>data-confirm<\/h4>\n<p>\u78ba\u8a8d\u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u8868\u793a\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h4>data-disabled-with<\/h4>\n<p>submit\u30dc\u30bf\u30f3\u306e\u5c5e\u6027\u306e\u4e00\u3064\u3067\u30d5\u30a9\u30fc\u30e0\u306e\u4e8c\u91cd\u30af\u30ea\u30c3\u30af\u3092\u9632\u6b62\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h2>Rails\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u5bfe\u7b56<\/h2>\n<p>Rails\u3067\u306fAjax\u30ea\u30af\u30a8\u30b9\u30c8\u5185\u306b\u3082\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30c8\u30fc\u30af\u30f3\u306e\u57cb\u3081\u8fbc\u307f\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u30c8\u30fc\u30af\u30f3\u3092\u57cb\u3081\u8fbc\u3080\u4ed5\u7d44\u307f<\/h3>\n<p>Ajax\u304c\u52d5\u304f\u753b\u9762\u5185\u306b\u3042\u3089\u304b\u3058\u3081Rails\u304c\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30c8\u30fc\u30af\u30f3\u3092\u51fa\u529b\u3057\u3066\u304a\u304d\u3001\u305d\u306e\u30c8\u30fc\u30af\u30f3\u60c5\u5831\u3092Ajax\u306eX-CSRF-Token\u3068\u3044\u3046HTTP\u30d8\u30c3\u30c0\u3067\u9001\u308b\u5f62\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\u30c8\u30fc\u30af\u30f3\u306e\u51fa\u529b<\/h4>\n<p><span style=\"color: #ff0000;\"><strong>csrf_meta_tags\u30d8\u30eb\u30d1\u30fc<\/strong><\/span>\u306b\u3066\u884c\u3044\u307e\u3059\u3002\u5171\u901a\u30ec\u30a4\u30a2\u30a6\u30c8\u306eapplication.html.erb\u3067\u3082\u3053\u306e\u30d8\u30eb\u30d1\u30fc\u3067\u30d8\u30c3\u30c0\u5185\u306b\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>\u30c8\u30fc\u30af\u30f3\u306e\u9001\u4fe1<\/h4>\n<p>Rails\u304c\u81ea\u52d5\u7684\u306b\u884c\u3046Ajax\u30ea\u30af\u30a8\u30b9\u30c8\u306f\u81ea\u52d5\u3067\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3057\u3001\u81ea\u5206\u3067Ajax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\u306f\u4e0b\u8a18\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">Rails.ajax()<\/pre>\n<p>\u305f\u3060\u3001\u4e0a\u8a18\u30e1\u30bd\u30c3\u30c9\u306f<span style=\"color: #ff0000;\"><strong>Content-Type\u3092\u300capplication\/x-www-form-urlencoded \u300d<\/strong><\/span>\u3067\u9001\u4fe1\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067JSON\u3067\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3057\u305f\u3044\u5834\u5408\u306f\u4e0d\u4fbf\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001jQuery\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f7f\u3046\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067Rails\u306e\u30c8\u30fc\u30af\u30f3\u57cb\u3081\u8fbc\u307f\u3092\u884c\u3063\u3066\u304f\u308c\u307e\u3059\u304c\u3001rails-ujs\u3088\u308a\u3082\u524d\u306bjQuery\u30e9\u30a4\u30d6\u30e9\u30ea\u30d5\u30a1\u30a4\u30eb\u304c\u8aad\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>SJR<\/h2>\n<p>Ajax\u901a\u4fe1\u3092\u3057\u305f\u5f8c\u306b\u305d\u306e\u7d50\u679c\u3092\u30d6\u30e9\u30a6\u30b6\u5074\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3068\u3057\u3066\u306f<span style=\"color: #ff0000;\"><strong>Ajax\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30dc\u30c7\u30a3\u306bJavaScript\u30b3\u30fc\u30c9\u3092\u8fd4\u5374\u3057\u3066\u305d\u308c\u3092\u30d6\u30e9\u30a6\u30b6\u5074\u3067\u5b9f\u884c\u3059\u308b\u3068\u3044\u3046\u65b9\u6cd5<\/strong><\/span>\u304c\u3042\u308a\u307e\u3059\u3002\uff08\u4ed6\u306b\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\u3092\u4f5c\u3063\u3066Ajax\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u7d50\u679c\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3057\u305f\u304c\u3002\uff09\u3053\u308c\u3092<span style=\"color: #ff0000;\"><strong>Server-generated JavaScript Responses(SJR)<\/strong><\/span>\u3068\u3044\u3044\u307e\u3059\u3002<\/p>\n<h3>\u524d\u63d0<\/h3>\n<ul>\n<li>\u30b5\u30fc\u30d0\u5074\u306f\u753b\u9762\u5074\u306e\u3069\u306eDOM\u306b\u3088\u3063\u3066\u767a\u884c\u3055\u308c\u305fAjax\u30ea\u30af\u30a8\u30b9\u30c8\u306a\u306e\u304b\u3092\u77e5\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3002<\/li>\n<\/ul>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u524d\u63d0\u304c\u3042\u308b\u305f\u3081\u4e88\u3081\u753b\u9762\u5074\u306b\u306f\u3069\u306e\u884c\u306e\u51e6\u7406\u306a\u306e\u304bID\u3092\u77e5\u3089\u305b\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3084\u30d8\u30eb\u30d1\u30fc\u3084\u30e2\u30c7\u30eb\u7b49\u306e\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u5b9f\u88c5\u3057\u305f\u51e6\u7406\u3092\u4f7f\u3048\u3066\u624b\u8efd\u306b\u5b9f\u88c5\u304c\u3067\u304d\u308b\u3002<\/li>\n<li>\u30d5\u30e9\u30b0\u30e1\u30f3\u30c8\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u3088\u308b\u51e6\u7406\u306e\u9ad8\u901f\u5316\u304c\u3067\u304d\u308b\u3002<\/li>\n<\/ul>\n<h3>\u30c7\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>1\u753b\u9762\u3067\u5b9f\u88c5\u3059\u308bJavaScript\u304c\u3042\u3061\u3053\u3061\u306b\u70b9\u5728\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u5171\u901a\u5316\u3057\u305a\u3089\u304f\u306a\u308a\u307e\u3059\u3002<\/li>\n<li>ES2015\u3067\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u304a\u3044\u3066Webpack\u3067\u4e92\u63db\u6027\u306e\u3042\u308bJS\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u306e\u3067ES2015\u304c\u4f7f\u3048\u306a\u3044\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u672c\u8a18\u4e8b\u3067\u306fRails\u3067\u306eAjax\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u3064\u3044\u3066\u306e\u89e3\u8aac\u3092\u884c\u3044\u307e\u3059\u3002 Rails\u3067\u306eAjax\u306e\u5b9f\u88c5 \u30d3\u30e5\u30fc\u5074 link_to\u30e1\u30bd\u30c3\u30c9\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002 remote: true \u3053\u308c\u3060\u3051\u3067\u30ea\u30f3\u30af\u3092\u30af\u30ea [&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\/7836"}],"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=7836"}],"version-history":[{"count":16,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7836\/revisions"}],"predecessor-version":[{"id":20532,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7836\/revisions\/20532"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7836"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}