# Setting a cooking from an eruby .rhtml file

**URL:** <https://www.ruby-forum.com/t/setting-a-cooking-from-an-eruby-rhtml-file/142661>\
**Category:** Ruby\
**Created:** [July 10, 2008, 4:35am UTC](https://www.ruby-forum.com/t/setting-a-cooking-from-an-eruby-rhtml-file/142661 "2008-07-10T04:35:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [July 10, 2008, 4:35am UTC](https://www.ruby-forum.com/t/setting-a-cooking-from-an-eruby-rhtml-file/142661/1 "2008-07-10T04:35:12Z")

</div>

I also tried inserting the cookings at an intermediate page that  
redirects, and I’m having no luck with that either, though I can simply  
use the plain CGI object to do it inside of a \<% %\> pair.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [July 10, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/setting-a-cooking-from-an-eruby-rhtml-file/142661/2 "2008-07-10T14:04:41Z")

</div>

I assume you call the eruby program as the cgi handler from your  
webserver  
(apache?).

The eruby program doesn’t have a ‘feature’ to modify http headers that  
it  
outputs with -Mc if I am not mistaken.

An easy solution would be to write a small wrapper:

---------------- myeruby.rb ---------------------  
require ‘erb’  
require ‘cgi’

class ErbSpace  
def set\_cookie(a\_cookie)  
@cookie ||= []  
@cookie \<\< a\_cookie  
end

def get\_cookies  
@cookie || false  
end

def get\_binding  
binding  
end  
end

template = ERB.new(File.read(ARGV[0]))  
es = ErbSpace.new  
content = template.result(es.get\_binding)

## cgi = CGI.new cgi.out(‘cookie’=\>es.get\_cookies) { content }

Now I have a set\_cookie method in the erb template that I can call  
anywhere in  
the script:

------------- test.rhtml ------------------  
\<% set\_cookie(CGI:🍪:new(‘username’, ‘someone’)) %\>

\<%= “A ruby string”.reverse %\>

## \<% set\_cookie(CGI:🍪:new(‘another\_cookie’, ‘test’)) %\>

----------- See if it works: ---------------

$ ruby myeruby.rb test.rhtml  
Content-Type: text/html  
Content-Length: 36  
Set-Cookie: username=someone; path=  
Set-Cookie: another\_cookie=test; path=

gnirts ybur A

## 

Now just change your webserver to call myeruby.rb instead of eruby for  
.rhtml  
files. Also this provides you a good starting point to add more methods  
to  
ErbSpace if you need.

See also:

ri CGI  
ri CGI::Cookie  
ri erb

Martin
