Search code examples
cssruby-on-railsruby-on-rails-3asset-pipeline

Passing multiple strings into stylesheet_link_tag


I know this must have a simple elegant answer, but somehow I can't figure out what it is. I'm trying to pass multiple stylesheets into stylesheet_link_tag, and it's coming out wrong.

When I directly call stylesheet_link_tag "aaa", "bbb", I get the following:

<link href="/assets/aaa.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/bbb.css" media="screen" rel="stylesheet" type="text/css" />

Great!

The problem is that I have a function in my application helper that takes a variable:

stylesheet_link_tag(css_includes)

If I call it with a single value, all seems fine:

> css_includes = "aaa"
> stylesheet_link_tag(css_includes)
>>>  <link href="/assets/aaa.css" media="screen" rel="stylesheet" type="text/css" />

Sweet!

But if I try to pass in multiple objects, it doesn't work:

> css_includes = "aaa", "bbb"
> stylesheet_link_tag(css_includes)
>>>  <link href="/assets/["aaa", "bbb"].css" media="screen" rel="stylesheet" type="text/css">

What's the correct syntax here?

Thanks!


Solution

  • You would need to use an each block :

    <% ["aaa", "bbb"].each do |name| %>
       stylesheet_link_tag(name)
    <% end %>
    

    However, I don't think this is the Rails way. You should probably include "aaa" and "bbb" in application.css with require or import if you use sass.