1

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.

Simone Carletti
177k50 gold badges371 silver badges370 bronze badges
asked Jun 28, 2009 at 5:02

2 Answers 2

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.

answered Jun 28, 2009 at 5:32
Sign up to request clarification or add additional context in comments.

5 Comments

gives me the same error. undefined method 'escape' for #<UsersController:0x4d403c4> any thoughts?
you need to require "CGI" so that you can use the escape method.
That is because you should use CGI.escape, not escape. Look closely at the snippet ;)
The same error appears if I "require 'CGI'" or "require 'cgi'"
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 code
2

What 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.

answered Jun 30, 2009 at 5:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.