79

I'm trying to style a rails link using css using the following code:

<%= link_to "Learn More", :controller => "menus", :action => "index", :class => "btn btn-inverse" %>

I would expect that this would create a link that looks like this:

<a href="menus/" class="btn btn-inverse">Learn More</a>

Instead, rails is rendering this -

<a href="/menus?class=btn+btn-inverse">Learn More</a>

Has anyone else had this problem / know what I'm doing wrong? I know I can avoid this problem by manually creating the anchor tag rather than using helper, but I was wondering if there was a way to pass the css class info to the helper itself. I'm using Rails 3.2.6.

Thanks!

2
  • What happens with :class => ["btn", "btn-inverse"] or :class => %w"btn btn-inverse" ? Commented Oct 22, 2012 at 17:10
  • 2
    You could also just use menus_path as the second argument to link_to instead of specifying the controller and action. Commented Oct 22, 2012 at 17:50

5 Answers 5

143

You have a syntax problem. Try this instead:

<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>

Some documentation for you to go further with the link_to Helper

They say:

Be careful when using the older argument style, as an extra literal hash is needed:

link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => <a href="/articles" class="article" id="news">Articles</a>

Leaving the hash off gives the wrong link:

link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => <a href="/articles/index/news?class=article">WRONG!</a>

I recommend you to use the URL helper generated following your routes configuration. In your case:

link_to "Learn More", menus_path, :class => "btn btn-inverse"

A little reminder on the Helpers generated:

# routes.rb
resources :users

# any view/controller
users_path #=> /users
edit_user_path(user) #=> /users/:id/edit
user_path(user) #=> /users/:id  (show action)
new_user_path(user) #=> /users/new
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I was missing that hash. That's exactly what I was looking for, thanks!
yeah, I know - it makes you wait a few minutes from when you ask the question. two more minutes!
5

Try new argument convention:

<%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>

Comments

3

if you do not have a controller action / route necessary for the link, you can pass nil as the placeholder and get the classes to apply as necessary

<%= link_to 'link verbiage', nil,  class: 'classes for action tag'%>

1 Comment

The case for this is when you are over riding the click event with something like Jquery.
0

I solved my problem by the way

<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>

Comments

0

This is how i solved it using another view engine, HAML just in case a fellow developer is having this need

%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class  => "fa fa-plus-circle"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.