-2

I want to pass the string 'header__button' and the response from method classify(f) as the argument class to link_to.

I tried

<%= link_to f, '/dashboard', class: 'header__button' classify(f) %>

but Ruby won't allow it. How can it be done?

Stefan
  • 109,145
  • 14
  • 143
  • 218
Himmators
  • 14,278
  • 36
  • 132
  • 223

5 Answers5

3

You can simply pass an array:

<%= link_to f, '/dashboard', class: ['header__button', classify(f)] %>

The documentation for tag and content_tag contains an example showing this usage (link_to calls content_tag to construct the <a> tag).

Stefan
  • 109,145
  • 14
  • 143
  • 218
  • This is the most elegant solution for OP's special case. (i.e. concatenating CSS classes in a Rails tag helper) – Daniel Rikowski Jul 15 '15 at 14:39
  • 1
    It actually is a documented feature: http://apidock.com/rails/ActionView/Helpers/TagHelper/tag / http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag – Daniel Rikowski Jul 15 '15 at 14:42
1

Try concatenating the string and method:

<%= link_to f, '/dashboard', class: 'header__button ' + classify(f) %>
Wes Foster
  • 8,770
  • 5
  • 42
  • 62
0

Try interpolation using "#{}" as follows:

<%= link_to f, '/dashboard', class: "header__button #{classify(f)}" %>

Note that you need to be using double quotes "#{}".

potashin
  • 44,205
  • 11
  • 83
  • 107
vee
  • 38,255
  • 7
  • 74
  • 78
0

You can also concatenate with <<:

<%= link_to f, '/dashboard', class: 'header__button ' << classify(f) %>
potashin
  • 44,205
  • 11
  • 83
  • 107
0

Will do the trick

<%= link_to f, '/dashboard', class: "header__button #{classify(f)}" %>
Nermin
  • 6,118
  • 13
  • 23