How to Execute Ruby Template Files (Erb) Without a Web Server from Command Line

How do I execute ruby template files (ERB) without a web server from command line?

You should have everything you need in your ruby/bin directory. On my (WinXP, Ruby 1.8.6) system, I have ruby/bin/erb.bat

erb.bat [switches] [inputfile]
-x print ruby script
-n print ruby script with line number
-v enable verbose mode
-d set $DEBUG to true
-r [library] load a library
-K [kcode] specify KANJI code-set
-S [safe_level] set $SAFE (0..4)
-T [trim_mode] specify trim_mode (0..2, -)
-P ignore lines which start with "%"

so erb your_erb_file.erb should write the result to STDOUT.

(EDIT: windows has erb.bat and just plain "erb". The .bat file is just a wrapper for erb, which I guess should make the same command work pretty much the same on any OS)

See the prag prog book discussion (starts about half-way down the page).

Note also that Jack Herrington wrote a whole book about code generation that uses Ruby/ERB.

How to run .erb files in browser with a customized directory?

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

In Rails how can I get the contents of a page without making an HTTP request?

This is how you get the contents of a page in the console. It might work for you:

require 'action_controller/integration'
app = ActionController::Integration::Session.new;
app.get('/path_to_your_page')
puts app.response.body

Best way to use ERB 'in browser' similar to opening an html in browser

In order to use the .erb templating engine you'll have to make sure that the server that serves the HTML page has Ruby installed, as well as some HTTP routing mechanism that responds to the request, processes the embedded Ruby, and renders the HTML result.

So to answer your question, if you want to use ERB, you will have to install Ruby on your web server at bare minimum.

You would save yourself a lot of time by using a minimal framework like Sinatra, but it is possible to build something from scratch in pure Ruby, since the ERB class is included in Ruby core.

Not sure how dynamic you need the page to be, or how much user input alters the page, but you may be able to do what you need with a combination of JavaScript/cookies/localstorage.

How do I run multiple lines of Ruby in html.erb file

It is unusual to define a method in an ERB file, so I recommend against it.

If you want to call a block like #each, you can do something like the following:

<% names.each do |name| %>
<%= name %>
<% end %>

Don't forget the <% end %>.

Getting ERB files rendered in my browser

Devise generally treats the "new" route as user login. From terminal run

rake routes | grep devise and look for something like:

new_user_session GET    /users/sign_in(.:format)                 devise/sessions#new

That will tell you the path you need to hit to get your devise new view.

Your routes should look like this:

Rails.application.routes.draw do
devise_for :users

root 'users/sign_in'
end

You obviously also need a users model set up in your application.
Please see devise documentation github.com/plataformatec/devise#starting-with-rails



Related Topics



Leave a reply



Submit