Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Using ERB without Rails


  • Please log in to reply
3 replies to this topic

#1 orrymr

orrymr

    Signalman

  • Members
  • 11 posts
  • LocationJohannesburg, South Africa

Posted 04 March 2014 - 11:53 AM

Hi all,

 

I'm trying to get a better understanding of how the erb gem works.

So far I've managed to create 2 files: index.html.erb and proc.rb (short for process).

 

In index.html.erb I have:

<html>
    <body>
        erh?
        <%= "this is a test" %>
    </body>
</html>

in proc.rb:

require 'erb'

template = File.read('index.html.erb')

renderer = ERB.new(template)

puts renderer.result()

when I run: "ruby proc.rb" I get the following results:

<html>
	<body>
		erh?
		thsdfsdfs 
		this is a test
	</body>
</html>

As expected.

 

 

My question is this: how do I get this to work in the context of a web-server? That is, how can I make it so that when a client requests my webpage it returns index.html which is the processed version of index.html.erb?



#2 Ohm

Ohm

    Driver

  • Moderators
  • 398 posts
  • LocationCopenhagen

Posted 05 March 2014 - 03:50 PM

Are you looking to implement your own web-server or what?

 

You might take a look at Sinatra (http://www.sinatrarb.com/) where you'd be able to do something like:

# app.rb
require 'sinatra'

get '/' do
  erb 'index.html.erb'
end

and then have your code in index.html.erb be rendered like the above.


Blog: http://ohm.sh | Twitter: @madsohm

#3 orrymr

orrymr

    Signalman

  • Members
  • 11 posts
  • LocationJohannesburg, South Africa

Posted 09 March 2014 - 08:18 AM

I want to understand how the web-server knows when to run Ruby code. In the example above, is Sinatra the web-server? And if so, is it set up to listen on port 80 and is then configured to run app.rb?



#4 Ohm

Ohm

    Driver

  • Moderators
  • 398 posts
  • LocationCopenhagen

Posted 10 March 2014 - 07:33 AM

If you're interested in web-servers you should have a look at either Thin (http://code.macournoyer.com/thin/) or WEBrick (http://www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick.html)

 

Both Rails and Sinatra are the application layer of the web service, that is, they process the request, but are not in charge of receiving it - and it's not them who have to listen to port 80 (or 3000 if running e.g. Thin in development mode). 


Blog: http://ohm.sh | Twitter: @madsohm




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users