-1

How can I run .erb files in the browser? It is not in the Rails framework so I can't run in terminal the "rails server" command.

It is like a customized directory with .erb files.

This is the directory structure:

enter image description here

Do I need a webserver to run it like WEBrick for me to see it in localhost? How can I access it?

Thanks.

marknt15
  • 5,047
  • 14
  • 59
  • 67
  • 1
    erb is a templating system. It need to parsed before sending to the browser if it is to be useful. Could you clarify what you mean by 'run .erb files in the browser'? – muttonlamb Sep 07 '13 at 06:58
  • @muttonlamb: What I mean by run .erb files in the browser is to access it in the browser and show the output of the .erb file. Example a simple print of 'hello world'. I need to access something like = localhost/print_hello.html.erb Thanks – marknt15 Sep 07 '13 at 07:19
  • But the process is different: template engines must parse the erb file and generate a html file, browsers are not able to consume erb ... – mliebelt Sep 07 '13 at 09:03

1 Answers1

4

Are you using a different Ruby framework or need to access server data? If not, you can generate HTML from erb with the command line, then simply open the HTML:

<% # page.erb %>
<html>
  <head>
    <title>My erb</title>
  </head>
  <body>
    <h1>it is: <%= Time.now %></h1>
  </body>
</html>

To compile and open from the command line:

$ erb page.erb > page.html
$ open page.html 
cmpolis
  • 3,051
  • 21
  • 19
  • Sorry I don't know if it uses a different Ruby framework or if it uses the native Ruby to create the .erb files. Do I need to open the file in the terminal and not accessed in the browser in order to view the file? Thanks @cmpolis – marknt15 Sep 07 '13 at 07:25
  • Running `erb page.erb > page.html` turns `page.erb` into an html file(`page.html`) that you can open with a browser – cmpolis Sep 07 '13 at 07:30
  • Thanks for the reply. I need to run the existing page_name.html.erb in the browser (like enter localhost/page_name in the URL) and not convert .erb files to make it .html files. – marknt15 Sep 07 '13 at 07:42
  • 2
    Browsers are made for opening html files, just open the local file. If you need to run it through localhost, then you will need to run a webserver... – cmpolis Sep 07 '13 at 07:45
  • Hi cmpolis. The system I'm working on is complicated. It is built in RoR but the source code for controllers and other stuffs are hidden. It only generates the views and other public files for editing in production. That's why I thought it was not a RoR framework. Thanks for clarifying :) – marknt15 Sep 17 '13 at 03:04