1

Say I have a text "favorite video url" field in my app, I need a way to tell rails that this is a url and then get be able to get parameters from it,

for instance, if the user inputs http://www.youtube.com/watch?v=RV0-DgsDLhs

I want to be able to get the 'v'param and store it in a variable.

Please help

nacho10f
  • 5,816
  • 6
  • 42
  • 73
  • possible duplicate of [How to extract URL parameters from a URL with Ruby or Rails?](http://stackoverflow.com/questions/2500462/how-to-extract-url-parameters-from-a-url-with-ruby-or-rails) – mechanical_meat Nov 11 '10 at 05:56

1 Answers1

7
require 'uri'
require 'cgi'

uri = 'http://www.youtube.com/watch?v=RV0-DgsDLhs'

params = CGI::parse( URI::parse(uri).query )

puts params.inspect # => {"v"=>["RV0-DgsDLhs"]}
puts params['v'][0] # => RV0-DgsDLhs

Why some of this functionality is in URI and some of it is in CGI is beyond me, but there you have it.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182