I'm new to rails so I apologize for my ignorance.
I'm setting a constant in a class outside of a method:
PARAM = { #... => ...
'field' => escape('somethingwith/slashes')
}
and get a NoMethodError: undefined method 'escape'
I tried Rack::Utils::escape and Rack::Utils.escape instead, but both don't work.
Thanks in advance.
2 Answers 2
You can use CGI.escape.
# lib/my_foo
class MyFoo
THINGS = {
:hi => CGI.escape("well hello, there.")
}
end
If yo do this outside of the Rails environment, you'll have to require "cgi" as well.
5 Comments
CGI.escape, not escape. Look closely at the snippet ;)CGI.escape works for me. The error you pasted above tells me that escape was called on an instance of UsersController, not on CGI. Double check your codeWhat version of Rails are you using. If you're using Rails 2.3, you should have Rack available. Check this out:
>> require "rack" # Rails 2.3 and above has already done this
=> true
>> Rack::Utils.escape("the quick brown fox")
=> "the+quick+brown+fox"
If you're using a version of Rails older than 2.3, you'll need to install and require Rack yourself.
sudo gem install rack
Or, if you're managing gems from inside Rails, add the following line to your environment.rb inside the Initializer block:
config.gem "rack", "1.0.0"
Once you upgrade to Rails 2.3 or higher, you'll be able to use the version of Rack built-in with Rails, and you can remove the config.gem line.