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
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
How Does This Block Work for Integer Times Method
Undefined Method 'Email' for Nil:Nilclass in Exibe the Mail of Table Father
Ruby 1.8: Hash#Sort Not Return Hash But Array (Better Way to Do This)
How to Use Ruby Date Constants Gregorian, Julian, England and Even Italy
Why Do We Need Nginx with Thin on Production Setup
How to Recursively Flatten a Yaml File into a JSON Object Where Keys Are Dot Separated Strings
How to Get a List from a Ruby Enumerable
Bundle Install Is Using a Different Ruby Version
How to Set the Mechanize Page Encoding
What Is a More Ruby-Like Way of Doing This Command
Import CSV in Batches of Lines in Rails
Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays
Initialize the Delayed Jobs Gem by Starting the Workers on Application Start
Loop Within Loop in Rails Controller
Split Float into Integer and Decimals in Ruby