user1751210
user1751210

Reputation: 31

Custom Javascript type with Rails and Slim

In Rails, both the javascript_tag and javascript_include_tag render the type attribute as

type="text/javascript"

I need to customize this attribute. Our application uses Slim syntax which makes this a little more difficult.

script src="mathjax_config" type="text/x-mathjax-config"

^ doesn't error but doesn't even include the file

javascript_tag[type="text/x-mathjax-config"] javascript code here

^ throws an error

I'm hoping to avoid breaking the file away from slim.

Ruby version 1.9.3 Rails version 3.2.3

Upvotes: 3

Views: 4820

Answers (2)

Alexander Presber
Alexander Presber

Reputation: 6635

The file not being included is not Slims fault. Slim is agnostic to the content of the attributes, so it will happily render

script src="mathjax_config" type="text/x-mathjax-config"

into

<script src="mathjax_config" type="text/x-mathjax-config"></script>

Also, as far as I know, the browsers should happily load scripts with types other than "text/javascript" and you should be able to access them using their id.

Your approach is correct and the script not being included must have a different reason (like the file specified in src cannot be found).

Upvotes: 1

Jim Wrubel
Jim Wrubel

Reputation: 4481

Since you are modifying the type attribute you would either need to override the javascript_tag helper or write your own for that to work. This syntax isn't as elegant but should work:

script src=asset_path("mathjax_config") type="text/x-mathjax-config"

Note that this is slim/haml syntax - YMMV in erb.

Upvotes: 1

Related Questions