0

I want to get some specific value from string of Rails like below these are two string.

 1. "http://localhost:3000/admin/shops/assign_shoes?id=50&page=1"

 2. "http://localhost:3000/admin/shops/assign_shoes?id=50"

I always need value of "id" which is "50". Don't matter how many parameters are in string

as query string.

Actually these strings are values of request.referer

Any efficient method?

Thanks

9
  • I do not understand what you mean Commented Jan 31, 2014 at 17:33
  • I want to get value of id. which is 50 in both strings. Commented Jan 31, 2014 at 17:36
  • id = 50 see it is in query string. Commented Jan 31, 2014 at 17:36
  • To be clear, are you saying your controller (for example) is being passed a string of a URL, and you need to parse out the query parameters? Or are you talking about how to get parameters in a normal HTTP GET request? Commented Jan 31, 2014 at 17:36
  • @Teeg yes you are right. Commented Jan 31, 2014 at 17:37

3 Answers 3

4

Here is one of the ways:

require 'uri'
require 'cgi'

uri = URI.parse("http://localhost:3000/admin/shops/assign_shoes?id=50&page=1")
# => #<URI::HTTP:0x000001018dc5c8 URL:http://localhost:3000/admin/shops/assign_shoes?id=50&page=1> 

uri_params = CGI.parse(uri.query)
# => {"id"=>["50"], "page"=>["1"]} 

uri_params["id"].first #=> "50" - NOTE: this will be a String!!

However, I'd prefer the answer which uses regular expressions.

Sign up to request clarification or add additional context in comments.

3 Comments

Beat me to it. Just a quick note, in my test I did not have to require 'cgi' in rails, which is what the OP appears to be using. It seems the module is already included, I believe.
Aah, I did it in irb. So, had to require both uri and cgi. :/
Makes sense. That's what I figured.
2

Use regular expressions.

id = /\/admin\/shops\/assign_shoes\?id=(\d+)/.match(request.referer)[1]

2 Comments

I would NOT use regular expressions to parse URLs, because there are perfectly good URL parsers already available to every Rails application (Rack::Utils), and the regular expression will be more brittle. For example, you don't really care about the path, but if you change it, your regular expression will break. It will also be difficult to make a regular expression that accepts the id parameter at any position.
I agree that it is better to use the URL parsers.
1

There are several different ways to do this, the common way I know about is parsing the string using the URI class, and making use of the CGI class to extract the query params, like so:

uri = URI.parse(request.referer)
parsed_query = CGI::parse(uri.query).symbolize_keys
id_value = parsed_query[:id].first

Note the .first, as the values of the query params are resolved to arrays. Additionally, the keys are parsed in to strings, therefore I would include symbolize_keys for convenience and consistency.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.