Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to require a class in an erb template?

I have an erb template in which I need to use:

CGI.unescapeHTML(someEscapedHTML)

So I need to require 'cgi', however the following fails:

<% require 'cgi' %>

With the error:

can't dup NilClass

like image 561
Undistraction Avatar asked May 20 '12 21:05

Undistraction


2 Answers

I would personally never put a require statement in a view, because 1) it's ugly and 2) what if another view needed that require?

A better place for this is in config/application.rb at the bottom, or in a file in config/initializers.

like image 181
Ryan Bigg Avatar answered Oct 16 '22 10:10

Ryan Bigg


First of all do not require gems or libraries in ERB please. Then CGI is required by Rails itself already.

If you want to prevent Rails 3 from auto-escaping consider using

<%= data.html_safe %>

instead.

like image 3
iltempo Avatar answered Oct 16 '22 11:10

iltempo