user117046
user117046

Reputation: 577

nomethoderror for rack::utils::escape

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.

Upvotes: 1

Views: 1231

Answers (2)

Yehuda Katz
Yehuda Katz

Reputation: 28703

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.

Upvotes: 2

August Lilleaas
August Lilleaas

Reputation: 54603

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.

Upvotes: 2

Related Questions