Inserting Hyperlinks

  • HTML links are defined with the <a> tag.
  • The destination of the link is defined with the href attribute, which is enclosed within quotes.
  • The tag is closed using the </a> tag.

Creating a hyperlink:

<a href="http://example.com">This is a link</a>

The preceding example produces a hyperlink that displays "This is a link." When this link is clicked, the browser puts in a request to the website http://www.example.com.

Hyperlinks in Rails

When you using the Ruby on Rails Framework (which will be discussed in depth as you proceed) you are able to embed Ruby code into your HTML markup. To embed Ruby within your HTML document you must:

  • Give the document a compatible file extension, such as .html.erb
  • Place the Ruby code within the <% and %> brackets
  • Use an equal sign to display the Ruby code within the brackets
<%= code to be displayed %>
  • Install Ruby on Rails onto your local computer or Web Server to process the document

For example:

<%= link_to "This is a link", "http://www.example.com" %>

Here we are creating a link that will be the functional equivalent of the "raw" HTML hyperlink example above. This link will be displayed by the browser as "This is a link". Clicking it sends a request via your web browser to the server that hosts the website http://www.example.com. Don't worry if this makes little sense right now, the important thing is that you understand that Ruby - the programming language behind the Ruby on
Rails web framework - can be embedded in an HTML document.

Also available in: HTML TXT