{"id":985,"date":"2012-12-10T13:06:36","date_gmt":"2012-12-10T04:06:36","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=985"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"url-%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%81%ae%e4%b8%80%e9%83%a8%e3%82%92%e9%81%b7%e7%a7%bb%e5%85%88%e3%81%ab%e5%bc%95%e3%81%8d%e7%b6%99%e3%81%90","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/12\/10\/url-%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%81%ae%e4%b8%80%e9%83%a8%e3%82%92%e9%81%b7%e7%a7%bb%e5%85%88%e3%81%ab%e5%bc%95%e3%81%8d%e7%b6%99%e3%81%90\/","title":{"rendered":"URL \u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e00\u90e8\u3092\u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def take_params\r\n  {:page =&gt; params[:page]}\r\nend<\/pre>\n<p>\u3088\u304f\u3042\u308b\u5546\u54c1\u691c\u7d22\u30da\u30fc\u30b8\u3092\u60f3\u50cf\u3057\u3066\u3044\u305f\u3060\u304d\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u300c\u8ca1\u5e03\u300d\u3067\u691c\u7d22\u3057\u3066\uff08\u3044\u307e\u8ca1\u5e03\u304c\u6b32\u3057\u3044\u3093\u3067\u3059\uff09\u3001\u5546\u54c1\u4e00\u89a7\u304c\u8868\u793a\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u691c\u7d22\u306b\u30d2\u30c3\u30c8\u3057\u305f\u5546\u54c1\u304c\u591a\u3044\u306e\u3067\u3001\u5546\u54c1\u4e00\u89a7\u306f\u30da\u30fc\u30b8\u30f3\u30b0\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>794 \u30da\u30fc\u30b8\u3067\u3084\u3063\u3068\u826f\u3055\u305d\u3046\u306a\u5546\u54c1\u3092\u767a\u898b\u3057\u305f\u306e\u3067\u30af\u30ea\u30c3\u30af\u3057\u3066\u8a73\u7d30\u30da\u30fc\u30b8\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>\u4ed6\u306e\u5546\u54c1\u3082\u898b\u305f\u3044\u306e\u3067\u300c\u4e00\u89a7\u306b\u623b\u308b\u300d\u30ea\u30f3\u30af\u3092\u62bc\u3057\u305f\u3089\u5546\u54c1\u4e00\u89a7\u306e 1 \u30da\u30fc\u30b8\u306b\u623b\u3063\u3066\u3057\u307e\u3063\u305f\u3002<\/p>\n<p>\u672c\u5f53\u306f 794 \u30da\u30fc\u30b8\u3092\u8868\u793a\u3057\u3066\u3044\u305f\u3042\u306e\u9803\u306b\u623b\u308a\u305f\u304b\u3063\u305f\u306e\u306b\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>794 \u30da\u30fc\u30b8\u3092\u8868\u793a\u3057\u3066\u3044\u305f\u3042\u306e\u9803\u306b\u623b\u308a\u305f\u3044<\/h2>\n<p>\u304a\u305d\u3089\u304f View \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306e\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u5546\u54c1\u4e00\u89a7\u304b\u3089\u5546\u54c1\u8a73\u7d30\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u5546\u54c1\u8a73\u7d30', item_path(item)) %&gt;\r\n\r\n# \u5546\u54c1\u8a73\u7d30\u304b\u3089\u5546\u54c1\u4e00\u89a7\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u4e00\u89a7\u306b\u623b\u308b', items_path) %&gt;<\/pre>\n<p>\u8868\u793a\u3059\u308b\u30da\u30fc\u30b8\u756a\u53f7\u304c \/items?page=794 \u306e\u3088\u3046\u306b\u6307\u5b9a\u3055\u308c\u308b\u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b params[:page] \u3092\u30ea\u30f3\u30af\u5148\u306b\u5f15\u304d\u7d99\u3044\u3067\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u5546\u54c1\u4e00\u89a7\u304b\u3089\u5546\u54c1\u8a73\u7d30\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u5546\u54c1\u8a73\u7d30', item_path(item, :page =&gt; params[:page])) %&gt;\r\n\r\n# \u5546\u54c1\u8a73\u7d30\u304b\u3089\u5546\u54c1\u4e00\u89a7\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u4e00\u89a7\u306b\u623b\u308b', items_path(:page =&gt; params[:page])) %&gt;<\/pre>\n<p>\u3053\u308c\u3067\u8a73\u7d30\u30da\u30fc\u30b8\u304b\u3089\u4e00\u89a7\u30da\u30fc\u30b8\u306b\u623b\u3063\u305f\u3068\u304d\u306b\u3001\u4ee5\u524d\u898b\u3066\u3044\u305f794\u30da\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3068\u306f\u3044\u3048\u3001\u6025\u6fc0\u306b\u9762\u5012\u304f\u3055\u304f\u306a\u3063\u305f\u611f\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30e1\u30bd\u30c3\u30c9\u306b\u5207\u308a\u51fa\u3057\u3066\u307f\u308b<\/h2>\n<p>\u305d\u3053\u3067\u5192\u982d\u3067\u7d39\u4ecb\u3057\u305f take_params \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>take_params \u3092\u4f7f\u3046\u3068\u3001\u6b21\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u5546\u54c1\u4e00\u89a7\u304b\u3089\u5546\u54c1\u8a73\u7d30\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u5546\u54c1\u8a73\u7d30', item_path(item, take_params)) %&gt;\r\n\r\n# \u5546\u54c1\u8a73\u7d30\u304b\u3089\u5546\u54c1\u4e00\u89a7\u3078\u306e\u30ea\u30f3\u30af.\r\n&lt;%= link_to('\u4e00\u89a7\u306b\u623b\u308b', items_path(take_params)) %&gt;<\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9\u306b\u3057\u305f\u3060\u3051\u3058\u3083\u306a\u3044\u304b\u3001\u3068\u601d\u308f\u308c\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u30e1\u30bd\u30c3\u30c9\u306b\u3057\u3066\u3044\u308b\u304b\u3089\u3053\u305d\u5909\u66f4\u306b\u5f37\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u30da\u30fc\u30b8\u3042\u305f\u308a\u306e\u8868\u793a\u4ef6\u6570 (params[:per_page]) \u3082\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u6307\u5b9a\u3055\u308c\u308b\u3093\u3060\u3063\u305f\uff01\u300d\u3068\u5f8c\u304b\u3089\u6c17\u4ed8\u3044\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u3068\u304d\u306f\u3001take_params \u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3059\u308b\u3060\u3051\u3067\u6e08\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def take_params\r\n  {:page     =&gt; params[:page],\r\n   :per_page =&gt; params[:per_page]}\r\nend<\/pre>\n<p>\u300c\u691c\u7d22\u6761\u4ef6 (params[:search]) \u3082\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u6307\u5b9a\u3055\u308c\u308b\u3093\u3060\u3063\u305f!\u300d\u3068\u3044\u3046\u3068\u304d\u306f\u3001\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def take_params\r\n  {:page     =&gt; params[:page],\r\n   :per_page =&gt; params[:per_page],\r\n   :search   =&gt; params[:search]}\r\nend<\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9\u306b\u5207\u308a\u51fa\u3057\u305f\u3053\u3068\u3067\u3001\u5909\u66f4\u304c\u3042\u3063\u3066\u3082\u7c21\u5358\u306b\u5bfe\u5fdc\u51fa\u6765\u307e\u3057\u305f\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u5192\u982d\u3067\u7d39\u4ecb\u3057\u305f take_params \u306f\u3071\u3063\u3068\u898b\u3066\u5206\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u4f5c\u308a\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u306e\u307e\u307e\u3067\u306f\u82e5\u5e72\u6a5f\u80fd\u4e0d\u8db3\u3067\u3059\u306e\u3067\u3001\u3082\u3046\u5c11\u3057\u6539\u826f\u3092\u884c\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u6700\u7d42\u7684\u306b\u306f\u3001take_params \u81ea\u4f53\u3092\u6539\u826f\u3057\u3001\u30d5\u30a9\u30fc\u30e0\uff08POST\uff09\u3067\u753b\u9762\u9077\u79fb\u3059\u308b\u5834\u5408\u3082\u8003\u616e\u3057\u3066 take_params_hidden_field_tags \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3082\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>take_params \u306f ApplicationController \u3067\u5b9a\u7fa9\u3057\u3001View \u3067\u3082\u4f7f\u3048\u308b\u3088\u3046\u306b\u300chelper_method :take_params\u300d\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class ApplicationController &lt; ActionController::Base\r\n\r\n  #\r\n  # take_params \u304c params \u304b\u3089\u53d6\u308a\u51fa\u3059\u30ad\u30fc\u306e\u30c7\u30d5\u30a9\u30eb\u30c8. \r\n  #\r\n  def default_take_param_keys\r\n    %w(page per_page)\r\n  end \r\n\r\n  #\r\n  # \u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf. \r\n  #\r\n  # ==== \u8a73\u7d30\r\n  # \u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30ea\u30f3\u30af\u6642\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u308b\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b. \r\n  #\r\n  # link_to('\u30e6\u30fc\u30b6\u4e00\u89a7', users_path(take_params))\r\n  # link_to('\u30e6\u30fc\u30b6\u8a73\u7d30', user_path(@user, take_params))\r\n  #\r\n  # ==== \u5f15\u6570\r\n  # \u547c\u3073\u51fa\u3057\u5f62\u5f0f\u306f\u4ee5\u4e0b\u306e\u901a\u308a. \r\n  #\r\n  # (1) take_params\r\n  # params \u304b\u3089 default_take_param_keys \u304c\u8fd4\u3059\u540d\u524d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059. \r\n  #\r\n  # (2) take_params(:key1, :key2)\r\n  # params \u304b\u3089 :key1, :key2 \u3092\u53d6\u308a\u51fa\u3059. \r\n  #\r\n  # (3) take_params(:additional_key1 =&gt; 1, :additional_key2 =&gt; 2)\r\n  # params \u304b\u3089 default_take_param_keys \u304c\u8fd4\u3059\u540d\u524d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059. \r\n  # \u305d\u306e\u7d50\u679c\u306b {:additional_key1 =&gt; 1, :additional_key2 =&gt; 2} \u3092\u30de\u30fc\u30b8\u3059\u308b. \r\n  #\r\n  # (4) take_params(:key1, :key2, :additional_key1 =&gt; 1, :additional_key2 =&gt; 2)\r\n  # params \u304b\u3089 :key1, :key2 \u3092\u53d6\u308a\u51fa\u3059. \r\n  # \u305d\u306e\u7d50\u679c\u306b {:additional_key1 =&gt; 1, :additional_key2 =&gt; 2} \u3092\u30de\u30fc\u30b8\u3059\u308b. \r\n  #\r\n  # ==== \u623b\u308a\u5024\r\n  # \u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b Hash.\r\n  #\r\n  def take_params(*param_keys)\r\n    overwrites = param_keys.extract_options!\r\n    param_keys = default_take_param_keys if param_keys.blank?\r\n    params.dup.extract!(*param_keys).update(overwrites)\r\n  end \r\n  helper_method :take_params\r\n\r\nend<\/pre>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf\u306f default_take_param_keys \u30e1\u30bd\u30c3\u30c9\u306b\u5206\u3051\u307e\u3057\u305f\uff08\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u3066\u5909\u66f4\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3067\u3059\uff09\u3002<\/p>\n<p>\u307e\u305f\u3001take_params \u30e1\u30bd\u30c3\u30c9\u306f\u3001\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u540d\u524d\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u3084\u3001\u8ffd\u52a0\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6307\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u6539\u826f\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>take_params_hidden_field_tags \u306f\u30d5\u30a9\u30fc\u30e0\uff08POST\uff09\u3067\u753b\u9762\u9077\u79fb\u3059\u308b\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3001ApplicationHelper \u306b\u5b9a\u7fa9\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">module ApplicationHelper\r\n\r\n  #\r\n  # \u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf. \r\n  #\r\n  # ==== \u8a73\u7d30\r\n  # take_params(*param_keys) \u306e\u7d50\u679c\u3092\u542b\u3080 &lt;input type=\"hidden\"&gt; \u3092\u8fd4\u3059. \r\n  # Ex. '&lt;input type=\"hidden\" ... \/&gt;&lt;input type=\"hidden\" ... \/&gt; ...'\r\n  #\r\n  def take_params_hidden_field_tags(*param_keys)\r\n    tags = take_params(*param_keys).map do |key, value|\r\n      hidden_field_tag(key, value)\r\n    end \r\n    tags.join.html_safe\r\n  end \r\n\r\nend<\/pre>\n<p>take_params_hidden_field_tags \u306f take_params \u306e\u623b\u308a\u5024\u3092\u5358\u7d14\u306b &lt;input type=\"hidden\" ... \/&gt; \u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f7f\u3044\u65b9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&lt;%= form_for(...) do |form| %&gt;\r\n  ...\r\n  &lt;%= take_params_hidden_field_tags %&gt;\r\n  &lt;%= form.submit %&gt;\r\n&lt;% end %&gt;<\/pre>\n<p>\u30d9\u30b9\u30c8\u306a\u3084\u308a\u65b9\u304b\u3069\u3046\u304b\u306f\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001\u3053\u306e\u3088\u3046\u306a\u65b9\u6cd5\u3082\u3042\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3092\u304a\u4f1d\u3048\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>Enjoy Rails!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\ndef take_params<br \/>\n  {:page =&gt; params[:page]}<br \/>\nend<br \/>\n\u3088\u304f\u3042\u308b\u5546\u54c1\u691c\u7d22\u30da\u30fc\u30b8\u3092\u60f3\u50cf\u3057\u3066\u3044\u305f\u3060\u304d\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u300c\u8ca1\u5e03\u300d\u3067\u691c\u7d22\u3057\u3066\uff08\u3044\u307e\u8ca1\u5e03\u304c\u6b32\u3057\u3044\u3093\u3067\u3059\uff09\u3001\u5546\u54c1\u4e00\u89a7\u304c\u8868\u793a\u3055\u308c\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/12\/10\/url-%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%81%ae%e4%b8%80%e9%83%a8%e3%82%92%e9%81%b7%e7%a7%bb%e5%85%88%e3%81%ab%e5%bc%95%e3%81%8d%e7%b6%99%e3%81%90\/\">\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\/985"}],"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=985"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":18005,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions\/18005"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}