Embedding Ruby Code in HTML

Embedding Ruby code in HTML?

You will need to use a templating engine like Ruby Templates (ERB). Here's an example. Rails uses ERB so you could easily do this in each of your templates.

How to execute a html embedded ruby code on local machine

To evaluate the Ruby code manually you can use the erb command.

Given a index.html.rhtml file with the following content:

<html><body>Hello <%= "from Ruby" %></body></html>

Running:

$ erb index.html.rhtml > index.html

Results in a index.html file with:

<html><body>Hello from Ruby</body></html>

This file can be viewed in a browser.

Another option is to use a web server to convert and serve the files. Here's a one liner starting WebBrick on port 3000 (assuming a index.html.rhtml file in the current directory):

$ ruby -rwebrick -e "WEBrick::HTTPServer.new(:DocumentRoot => '.', :Port => 3000).start"

Pointing your browser to http://localhost:3000/index.html.rhtml should show the page.

Linking Ruby with HTML Code

Client Side Ruby (Ruby on the browser?!)

If your question refers to executing Ruby code on the browser, like you would execute Javascript (the server isn't involved), than it's not really possible in the traditional sense.

However, Opal (or similar transpilers) will "compile" your Ruby code into Javascript, allowing you to execute the compiled "Ruby" (now javascript) code within the browser, just like native Javascript (sometimes even better, as Opal attempts to optimize your code).

Server Side Ruby

Ruby is often used to run code on the server rather than the browser. There are a number of solutions.

For example, you can use ERB templates - here's a good intro to ERB - as well as other Ruby template engines (i.e. the amazing Slim template engine) to run Ruby code in a similar way to PHP code (only easier to write and maintain).

However, Ruby is much more powerful than simple templates. There are whole servers and frameworks written in and for Ruby, allowing you very powerful server-side scripting.

The common frameworks:

  • Ruby on Rails is a very common framework many beginners often start with.

  • Sinarta is also a good starting point, usually favored by more experienced coders who tend to believe it's less heavy than Rails.

  • Using Rack directly (with no framework) is the hi-performance mainstream choice, but it requires more knowledge.

  • Read the following link for more common Http-Web frameworks and Benchmarks.

There are also more experimental, or less common, frameworks - usually ones focusing on real-time web applications, such as Volt and Plezi.

How to add Ruby into this HTML tag?

check if your file is some_file.html.erb
then you can use
<%=User.first.address%>
You can not use #{User.first.address} to insert something into html code

Embedding Ruby on Rails Project into HTML File

So you have a Static Html-Css website. You want to add a blog to it. Right?

The thing is, a Ruby on Rails project (eg: your blog) is not something 'additional' that you can add to a website.
It is a very powerful framework that allows you to build an ENTIRE website within the project.

Once you have a Rails project (your blog) running, you can put all your other existing static html, css, js files into the Rails PUBLIC folder.

Now if start up your server ( run rails server ), and try to access your other pages, you should see them. eg: ( localhost:3000/my-page-name.html )

Now to get localhost:3000 to point to your actual homepage.

In your 'routes.rb' file, add route:

root 'main#index'

Create a new Controller file in controllers:

 class MainController < ApplicationController
def index
redirect_to '/index.html'
end
end

You'll have to learn a couple of things about Rails if you're planning to move your entire website to Rails or if you're curious.
This should help you get started.
Or if you're not looking to learn an entire framework for a blog, try this.

Good luck!

Ruby on Rails Embed Ruby Code in Javascript inside Application Erb File

Change this:

<%= current_user_json %>

To this:

<%= current_user_json.html_safe %>

Heads up that you must ensure your json is properly escaped. For example, if your current_user_json happens to have a field with a quote in it, you must escape that quote. If you don't escape, then what you're doing is a pretty typical attack vector for hackers, so proceed with care.

How to insert code from Ruby into HTML?

You might want to have a look at Opal: http://opalrb.org/

There is no way to run Ruby natively in the browser. It has to be converted to JS at some point, either when the page is loaded or as a preprocess task when your site is deployed.

How to embed ruby/erb code into an inline html style tag?(progress bar)

You can try this style:
width: <%= assignment.completion %>



Related Topics



Leave a reply



Submit