Shulte
Shulte

Reputation: 33

convert url to readable string with ruby on rails

I using impressionist gem for user activity and store url's to referrer. How i can convert url to readable string? Here what i mean:

saved url: http://example.com/index?book_id%5B%5D=35&lang_id%5B%5D=1&category_id=3

need convert to readable names and show on impressions view like: 'Book Name', 'Language', 'Category Name'

Upvotes: 0

Views: 854

Answers (1)

BroiSatse
BroiSatse

Reputation: 44725

You can go with:

uri = URI.parse('http://example.com/index?book_id%5B%5D=35&lang_id%5B%5D=1&category_id=3')
params = Rack::Utils.parse_nested_query u.query
#=> {"book_id"=>["35"], "lang_id"=>["1"], "category_id"=>"3"}

Now you are left with translating keys to readable name. Normally you could use humanize method, however it won't work as requested:

'book_id'.humanize #=> 'Book'

Most likely you will need to fall back onto I18n to handle the translation.

Upvotes: 2

Related Questions