views:

228

answers:

2

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  A: 

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.

August Lilleaas
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.
Geo
That is because you should use `CGI.escape`, not `escape`. Look closely at the snippet ;)
August Lilleaas
The above error is using "CGI.escape".
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
August Lilleaas
+1  A: 

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.

Yehuda Katz