{"id":556,"date":"2009-01-08T10:42:44","date_gmt":"2009-01-08T01:42:44","guid":{"rendered":"http:\/\/www.gesource.jp\/weblog\/?p=556"},"modified":"2017-07-16T22:01:34","modified_gmt":"2017-07-16T13:01:34","slug":"ruby_on_rails-2","status":"publish","type":"post","link":"https:\/\/www.gesource.jp\/weblog\/?p=556","title":{"rendered":"Ruby on Rails\u306b\u3088\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306e\u9023\u52d5"},"content":{"rendered":"<p>\u8cea\u554f\u3092\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002<\/p>\n<blockquote><p>\n  \u8cea\u554f\u304a\u9858\u3044\u3057\u307e\u3059\u3002<br \/>\n  \uff12\u3064\u306e\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u3092\u9023\u52d5\u3055\u305b\u305f\u3044\u306e\u3067\u3059\u304c\u3044\u3044\u65b9\u6cd5\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f\n<\/p><\/blockquote>\n<p>Ruby on Rails\u306b\u3088\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306e\u9023\u52d5\u306e\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002<br \/>\n\u30b5\u30f3\u30d7\u30eb\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u308f\u304b\u308a\u3084\u3059\u3055\u3092\u91cd\u8996\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>View\u306e\u65b9\u3067\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u3092\u64cd\u4f5c\u3059\u308bJavaScript\u306e\u95a2\u6570\u3092\u7528\u610f\u3057\u3066\u304a\u304d\u3001<br \/>\nController\u304b\u3089JavaScript\u306e\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3053\u306e\u3042\u305f\u308a\u306e\u51e6\u7406\u306f\u3001\u3044\u308d\u3044\u308d\u306a\u3084\u308a\u65b9\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>index.html.erb<\/p>\n<pre><code>&lt;%= javascript_include_tag(:defaults) %&gt;\n&lt;script&gt;\n\/\/\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u304c\u9078\u629e\u3055\u308c\u305f\u3068\u304d\u306b\u547c\u3073\u51fa\u3055\u308c\u308b\u95a2\u6570\nfunction change_item() {\n  &lt;%= remote_function(:url =&gt; {:action =&gt; 'call_ajax'}, \n                      :with =&gt; \"'id=' + $('item1').value\") %&gt;\n}\n\/\/\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306e\u9078\u629e\u80a2\u3092\u524a\u9664\u3059\u308b\u95a2\u6570\nfunction clear_item() {\n  $('item2').length = 0;\n}\n\/\/\u30d6\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306b\u9078\u629e\u80a2\u3092\u8ffd\u52a0\u3059\u308b\u95a2\u6570\nfunction update_menu(name, value) {\n  $('item2').options[$('item2').options.length] = new Option(name, value);\n}\n&lt;\/script&gt;\n&lt;% form_tag do %&gt;\n&lt;%= select_tag('item1',\n               options_for_select([['x','1'],['y','2'],['z','3']]), \n               :onchange =&gt; 'change_item()') %&gt;\n&lt;%= select_tag('item2',options_for_select([])) %&gt;\n&lt;% end %&gt;\n<\/code><\/pre>\n<p>index_controller.rb<\/p>\n<pre><code>class IndexController &lt; ApplicationController\n  def index\n  end\n\n  def call_ajax\n    # \u9078\u629e\u80a2\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\n    item_list = {\n      '1' =&gt; [['x1', '1'], ['x2', '2'], ['x3', '3']],\n      '2' =&gt; [['y1', '4'], ['y2', '5'], ['y3', '6']],\n      '3' =&gt; [['z1', '7'], ['z2', '8'], ['z3', '9']]\n    }\n    items = item_list[params['id']]\n    # Javascript\u306e\u547c\u3073\u51fa\u3057\n    render :update do |page|\n      #\u9078\u629e\u80a2\u3092\u524a\u9664\n      page.call 'clear_item'\n      #\u9078\u629e\u80a2\u3092\u8ffd\u52a0\n      items.each { |item| page.call 'update_menu', item[0], item[1] }\n    end\n  end\nend\n<\/code><\/pre>\n<p><iframe loading=\"lazy\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?t=gesource-22&#038;o=9&#038;p=15&#038;l=st1&#038;mode=books-jp&#038;search=Ruby%20on%20Rails&#038;fc1=000000&#038;lt1=_blank&#038;lc1=3366FF&#038;bg1=FFFFFF&#038;f=ifr\" marginwidth=\"0\" marginheight=\"0\" width=\"468\" height=\"240\" border=\"0\" frameborder=\"0\" style=\"border:none;\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8cea\u554f\u3092\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002 \u8cea\u554f\u304a\u9858\u3044\u3057\u307e\u3059\u3002 \uff12\u3064\u306e\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u3092\u9023\u52d5\u3055\u305b\u305f\u3044\u306e\u3067\u3059\u304c\u3044\u3044\u65b9\u6cd5\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f Ruby on Rails\u306b\u3088\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306e\u9023\u52d5\u306e\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002 \u30b5\u30f3\u30d7\u30eb\u3068\u3044\u3046\u3053 &#8230;<\/p>\n<p><a href=\"https:\/\/www.gesource.jp\/weblog\/?p=556\" class=\"more-link\">Continue reading &lsquo;Ruby on Rails\u306b\u3088\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u306e\u9023\u52d5&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[142,70,42],"class_list":["post-556","post","type-post","status-publish","format-standard","hentry","category-ruby","tag-ruby","tag-ruby-on-rails","tag-tips"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=556"}],"version-history":[{"count":0,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/556\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}