sergserg
sergserg

Reputation: 22254

How can I generate a link to a controller action in Rails 3.2.7?

Here's the HTML I'd like to generate:

<li><a href="/Home/Index">HOME</a></li>

Meaning, a link to the Index action of the Home controller.

How can I achieve this in Rails without generating the entire <a> element?

Does something like this exist?

<li><a href="<%= link_tag(:controller => "home", :action => "index") %>">HOME</a></li>

Upvotes: 0

Views: 74

Answers (2)

JGrubb
JGrubb

Reputation: 889

link_to generates all the markup, so you don't need to write it inside an <a> tag.

<%= link_to 'Home', {:controller => :home, :action => :index}, :class => "awesome", :id => "super-awesome" %>

spits out

<a href="/home/index" class="awesome" id="super-awesome">Home</a>

Edit: Also, this is your friend - http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

Upvotes: 3

Cody Caughlan
Cody Caughlan

Reputation: 32748

Why not use the link_to helper? If your route is named home than:

link_to('Home', home_path)

will generate

<a href="/Home/Index">HOME</a>

You can retrieve your route names by running rake routes from the command line.

Upvotes: 0

Related Questions