Categories
Rails

Ruby on Rails: links

The official format is:

link_to(name, options={}, html_options={})

In the templates, you can use it as:

<%= link_to “Home”,    ‘home_path’ %>

Everywhere else (example):

link_to 'New', {controller: 'articles', action: 'new'}, {class: 'large'}

First argument: The text that will show as part of the link

Second argument: the name of the route we are invoking

Third argument: options, anything else you need to pass to the links

You can also encapsulate a bunch of DOM elements with it:

<%= link_to new_project_path do %>
  More DOM elements and / or text in here
<% end %>

Leave a Reply

Your email address will not be published. Required fields are marked *