{"id":57367,"date":"2022-12-27T09:19:11","date_gmt":"2022-12-27T00:19:11","guid":{"rendered":"https://freelance.indieverse.co.jp/media/?p=57367"},"modified":"2026-06-08T16:08:29","modified_gmt":"2026-06-08T07:08:29","slug":"ruby-get-url-parameter","status":"publish","type":"post","link":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-get-url-parameter","title":{"rendered":"RubyでURLパラメーターの値を取得する方法 | Addressableを利用して取得する"},"content":{"rendered":"<p>この記事では、Rubyを用いて、以下のようなURLから、特定のパラメーターの値を取得する方法について記載します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># idとempidを取得する</code>url = 'http://www.example.com?id=4&amp;empid=6'</pre>\n</div>\n<p>結論、以下のように取得可能です。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>url = Addressable::URI.parse('http://www.example.com?id=4&amp;empid=6')\r\nurl.query_values # {\"id\"=&gt;\"4\", \"empid\"=&gt;\"6\"}</code></pre>\n</div>\n<h2>環境</h2>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>rails -v\r\nRails 7.0.4\r\nruby -v\r\nruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]</code></pre>\n</div>\n<h2>RubyでURLパラメーターを取得する方法</h2>\n<p>Rubyでは、URLを取得するライブラリに、以下のようなものがあります。</p>\n<ul>\n<li><a href=\"https://github.com/ruby/uri\">URI</a></li>\n<li><a href=\"https://github.com/sporkmonger/addressable\">Addressable</a></li>\n</ul>\n<p>AddressableはURIの後継だそうです。</p>\n<p>URLとAddressableがありますが、個人的にはURLパラメーターの取り扱いがAdderssableの方が体験としてよかったため、Addressableを利用して、以下の値を取得する方法について見ていきます。</p>\n<p>※詳しくはAddressableのGithubに詳しい解説が書いてあるため、参照してください。</p>\n<p><a href=\"https://github.com/sporkmonger/addressable\">https://github.com/sporkmonger/addressable</a></p>\n<h2>Gem Addressableのインストール</h2>\n<p>まず、AddressableのGemをインストールします。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>$ gem install addressable\r\n</code></pre>\n</div>\n<h2>URLパラメーターの取得</h2>\n<p>次に、</p>\n<ul>\n<li>id</li>\n<li>empid</li>\n</ul>\n<p>の取得をしていきます。</p>\n<p>URLをパースした値に対して、<code>query_values</code>メソッドを呼ぶことで、ハッシュ形式でURLパラメーターを取得してくれます。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>url = Addressable::URI.parse('http://www.example.com?id=4&amp;empid=6')\r\n# query_valuesを利用すると、ハッシュでURLパラメーターを取得してくれる\r\nurl.query_values # {\"id\"=&gt;\"4\", \"empid\"=&gt;\"6\"}</code></pre>\n</div>\n<p>非常に簡単でした。</p>\n<h2>スキーマの取得</h2>\n<p>URLから、</p>\n<ul>\n<li>http</li>\n<li>https</li>\n</ul>\n<p>などの値を取得したい場合は、<code>schema</code>を指定することで取得可能です。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>uri = Addressable::URI.parse(\"http://example.com/path/to/resource/\")\r\nuri.scheme\r\n#=&gt; \"http\"</code></pre>\n<h2>ドメインの取得</h2>\n<p>URLから、</p>\n<ul>\n<li>example.com</li>\n</ul>\n<p>の値を取得したい場合は、<code>host</code>を指定することで取得可能です。</p>\n</div>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>uri = Addressable::URI.parse(\"http://example.com/path/to/resource/\")\r\nuri.host\r\n#=&gt; \"example.com\"</code></pre>\n<h2>相対パスの取得</h2>\n<p>URLから、</p>\n<ul>\n<li>/path/to/resource/</li>\n</ul>\n<p>を取得したい場合、<code>path</code>を指定することで取得できます。</p>\n</div>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>uri = Addressable::URI.parse(\"http://example.com/path/to/resource/\")\r\nuri.path\r\n#=&gt; \"/path/to/resource/\"</code></pre>\n</div>\n<div class=\"hcb_wrap\">\n<h2>備考：URIとの比較</h2>\n</div>\n<p>URIでも同じようなことを行うことができるのですが、個人的にはAddressableの方がURLパラメーターの取得が楽でした。</p>\n<p>参考までにURIのGithubのサンプルコードを掲載しておきます。</p>\n<p>URI:<br />\n<a href=\"https://github.com/ruby/uri\">https://github.com/ruby/uri</a></p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\">require \"uri\"\r\nuri = URI(\"http://foo.com/posts?id=30&amp;limit=5#time=1305298413\")\r\n#=&gt; #&lt;URI::HTTP http://foo.com/posts?id=30&amp;limit=5#time=1305298413&gt;\r\nuri.scheme #=&gt; \"http\" #スキーマの取得\r\nuri.host #=&gt; \"foo.com\" #ドメインの取得\r\nuri.path #=&gt; \"/posts\" #パスの取得\r\nuri.query #=&gt; \"id=30&amp;limit=5\" #URLパラメーターの取得\r\nuri.fragment #=&gt; \"time=1305298413\"\r\n<code></code></pre>\n</div>\n<div class=\"hcb_wrap\">\n<h2>参考</h2>\n</div>\n<ul>\n<li><a href=\"https://stackoverflow.com/questions/7316656/how-to-get-a-query-string-from-a-url-in-rails\">https://stackoverflow.com/questions/7316656/how-to-get-a-query-string-from-a-url-in-rails</a></li>\n<li><a href=\"https://github.com/sporkmonger/addressable\">https://github.com/sporkmonger/addressable</a></li>\n</ul>\n<h2>URLパラメーター処理をRails案件で活かすには</h2>\n<p>URLパラメーターの取得は、Ruby on Railsの実務でも検索機能、絞り込み、ページネーション、広告計測、外部サービス連携などで使われます。Railsでは<code>params</code>で扱う場面が多いものの、URLを直接パースする処理を理解しておくと、Webアプリのリクエスト処理をより深く理解できます。</p>\n<p>RailsのWebアプリ開発でどのような機能実装が求められているかを知りたい場合は、<a href=\"https://freelance.indieverse.co.jp/job_listings/skills/389\">Ruby on Rails案件一覧</a>で、実際の案件内容や必要スキルを確認してみましょう。</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n","protected":false},"excerpt":{"rendered":"<p>RubyでURLパラメーター取得を解説。Addressableのquery_valuesでidやempidを取得し、schemeやhost、pathの取得とURIとの違いも説明します。</p>\n","protected":false},"author":1,"featured_media":56300,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[190],"tags":[],"class_list":["post-57367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby"],"aioseo_notices":[],"meta_description":"RubyでURLパラメーター取得を解説。Addressableのquery_valuesでidやempidを取得し、schemeやhost、pathの取得とURIとの違いも説明します。","_links":{"self":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/57367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts"}],"about":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/types/post"}],"author":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/users/1"}],"replies":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/comments?post=57367"}],"version-history":[{"count":3,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/57367/revisions"}],"predecessor-version":[{"id":96840,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/57367/revisions/96840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media/56300"}],"wp:attachment":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media?parent=57367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/categories?post=57367"},{"taxonomy":"post_tag","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/tags?post=57367"}],"curies":[{"name":"wp","href":"https://api.w.org/{rel}","templated":true}]}}