[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Setting a cooking from an eruby .rhtml file

Xeno Campanoli

7/10/2008 2:31:00 AM

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.

3 Answers

Martin Boese

7/10/2008 11:58:00 AM

0

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::Cookie::new('username', 'someone')) %>


<html>

<%= "A ruby string".reverse %>


</html>
<% set_cookie(CGI::Cookie::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=




<html>

gnirts ybur A


</html>
-------------------

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



On Thursday 10 July 2008 03:30:49 Xeno Campanoli wrote:
> 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.



MattB

7/9/2012 10:30:00 PM

0

On Mon, 09 Jul 2012 16:18:13 -0600, Yoorghis@Jurgis.net wrote:

>On Mon, 09 Jul 2012 12:07:47 -0700, MattB
><trdell1234NOMORESPAM@gmail.com> wrote:
>
>>>Yep, and if you recall (unless a week or so is ancient history for
>>>you), the bragging about being a gunwhore and carrying a weapons to
>>>enforce the law was a post you made---not me.
>>
>>
>>Yes and did notice you cut it out
>
>Flushing shit is what's usually done.

Yoorghis wrote: Quote

""Lots of REAL americans would give him the same treatment he gave
Martin.

Then justice would be served.""

End Quote

Yoorghis

7/10/2012 1:48:00 AM

0

On Mon, 09 Jul 2012 15:29:50 -0700, MattB
<trdell1234NOMORESPAM@gmail.com> wrote:

>
>""Lots of REAL americans would give him the same treatment he gave
>Martin.

Then you're saying that Zimmerloon was justified in murdering an
innocent man---then not being arrested.