1

I have a user with name "k#arn". While passing that user through url it's taking only k.

"/users/auto_complete/?user_input=" + "k#arn"

above code is taking "k" as user name. Is there any way to escape # and pass the whole name ("k#arn") in the url?

Can Can
  • 3,644
  • 5
  • 32
  • 56

1 Answers1

3

You should url_encode that string:

require 'erb'

ERB::Util.url_encode('k#arn')
#=> "k%23arn"

It is not a good idea to build urls manually, whenever possible use Rails' named routes. In this example I would expect a route like auto_complete_users_path(user_input: 'k#arn'), but that depends on your use case and your routes file...

spickermann
  • 100,941
  • 9
  • 101
  • 131