nacho10f
nacho10f

Reputation: 5876

How to get url parameters from a url string?

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

Upvotes: 1

Views: 1989

Answers (1)

Jordan Running
Jordan Running

Reputation: 106017

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.

Upvotes: 7

Related Questions