Url encode


Can someone tell me how to encode a string to a url in Rails.
I’ve got basic code as below, but it only adds dashes in place of spaces
and doesn’t escape html.
@page.url = @page.name.to_s.gsub(/[ ]/, ‘-’)


@page.url = CGI::escape(@page.name)

Will do URL encoding for you. To decode, use CGI::unescape().

More info is available by checking out the CGI class on the ruby-docs