Url Encode Equivalent in Ruby on Rails

url encode equivalent in ruby on rails

I believe the u helper method is what you're looking for:

<%=u "URL ENCODE <p>ME</p>" %>

I can't seem to find the documentation for that method, but if I find it in the near future I'll be sure to put a link in here.

Edit: You can find the documentation for this method here.

Ruby equivalent to PHPs urlencode

Ruby uses UTF-8 strings by default:

str = 'ö'

str.encoding
#=> #<Encoding:UTF-8>

If you want an ISO 8859 encoded string in Ruby, you have to convert it:

str.encode('ISO-8859-1')
#=> "\xF6"

to URL-encode a string, there's CGI.escape:

require 'cgi'

CGI.escape(str.encode('ISO-8859-1'))
#=> "%F6"

to encode an URL, use URI.escape:

require 'uri'

url = 'http://example.com/this/is/an/ö'
URI.escape(url.encode('ISO-8859-1'))
#=> "http://example.com/this/is/an/%F6"

Simple cipher to encode URL

Use Base64.urlsafe_encode64

Base64 is a good idea to use, Ruby provides URL safe implementation of it, because the default will produce characters like + and =. urlsafe_encode64 will use '-' instead of '+' and '_' instead of '+' so they can be passed like URL parameters.

require "base64"

encoded_url = Base64.urlsafe_encode64('http://www.someurl.com', ) #=> "aHR0cDovL3d3dy5zb21ldXJsLmNvbQ=="
decoded_url = Base64.urlsafe_decode64(encoded_url) #=> "http://www.someurl.com"

https://apidock.com/ruby/Base64/urlsafe_encode64

Alternative method in Rails 4 for RawUrlEncode PHP method

How about URI::escape method?

URI::escape('google.com/?q=what an awesome day')

The method above escapes the input string, replacing all unsafe characters with codes. That is, it encodes the input using percent-encoding. You can read more about percent encoding:

http://en.wikipedia.org/wiki/Percent-encoding

Converting url encoded strings into plain text with ruby

You can use CGI::unescape for URL-decoding:

require 'cgi'
string = CGI::unescape("1+%2B+2+%3D+3")
#=> "1 + 2 = 3"

How to get params for url with whitespace as '%20' instead of '+' in Rails

I would recommend just sticking to the use of a gsub, perhaps with a comment to explain the need for such behaviour.

While you could solve the problem by use of URI.escape, it is supposedly deprecated, as it does not fully conform to RFC specs. See here for a great write-up on it.

Hash#to_param is an alias of Hash#to_query, which calls Object#to_query. The simplest example to demonstrate this + vs %20 issue is:

'John Key'.to_query(:name) # => "name=John+Key"

The implementation of Object#to_query is:

def to_query(key)
"#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
end

And so, we find that:

CGI.escape("John Key") # => "John+Key"

Hence, this is why I have referenced the differences between CGI.escape and URI.escape.

How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both?

Use

URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

in ruby, and

encodeURIComponent(foo); 

in javascript

Both these will behave equally and encode space as %20.

What is the Ruby/Rails equivalent of Python's urllib.quote_plus?

CGI::escape does just that:

irb(main):003:0> require 'cgi'
=> true
irb(main):004:0> CGI::escape("foo and/or bar")
=> "foo+and%2For+bar"


Related Topics



Leave a reply



Submit