How to get url parameters from a url string?
Say I have a text "favorite video url" field in my app, I need a w开发者_运维百科ay 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
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.
精彩评论