0

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'

4
  • 2
    You can use friendly_id gem. Commented Jul 28, 2015 at 8:27
  • @Pavan This is a great gem, but is it helps me to show all id's from url as names in view? Commented Jul 28, 2015 at 8:32
  • Yes. You can get the urls like http://example.com/index/rails/english/fremework Commented Jul 28, 2015 at 8:36
  • @Pavan - Note that some of the id params are arrays though. Commented Jul 28, 2015 at 8:45

1 Answer 1

2

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.

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

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.