Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally add a class to link_to in Rails

I want to conditionally add the class "hidden" to a Rails link tag, depending on if "accepted == true".

If I weren't using a rails link_to I could do <a href="#" class="foo bar <%= "hidden" if accepted == true %>" >. How can I accomplish this inside a link_to?

<%= link_to "Accept Friend Request", 
    "#", 
    class: "btn btn-success btn-sm btn-block requestSent ???hidden???",
    disabled: true %>
like image 267
Don P Avatar asked Mar 13 '14 19:03

Don P


1 Answers

You can do it outside the link_to:

<% css_class = accepted ? "hidden" : "" %>
<%= link_to "Accept Friend Request", 
  "#", 
  class: "btn btn-success btn-sm btn-block requestSent #{css_class}",
  disabled: true %>
like image 119
MurifoX Avatar answered Oct 18 '22 19:10

MurifoX