Custom Javascript type with Rails and Slim

二次信任 提交于 2019-12-23 18:54:19

问题


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


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/12926940/custom-javascript-type-with-rails-and-slim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!