如何在Rails中为javascript_tag帮助器自定义'type'属性?

时间:2013-09-10 22:26:18

标签: ruby-on-rails ruby ruby-on-rails-3 handlebars.js

如何在Rails中向type帮助器添加自定义javascript_tag属性?

<%= javascript_tag id: 'entry-template', type: 'text/x-handlebars-template' do %>
  <div class="entry">
    <h1>{{title}}</h1>
    <div class="body">
      {{body}}
    </div>
  </div>
<% end %>

以上回报:

<script id="entry-template" type="text/javascript">
  <div class="entry">
    <h1>{{title}}</h1>
    <div class="body">
      {{body}}
    </div>
  </div>
</script>

所需的输出是:

<script id="entry-template" type="text/x-handlebars-template">
  <div class="entry">
    <h1>{{title}}</h1>
    <div class="body">
      {{body}}
    </div>
  </div>
</script>

2 个答案:

答案 0 :(得分:2)

javascript_tag适用于,嗯,JavaScript,它的名字就是这么说的。如果您想使用<script>作为非JavaScript的容器,请手动编写:

<script id="entry-template" type="text/x-handlebars-template">
    alert('All is not good');
</script>

或者,如果您必须使用帮助程序,请使用content_tag

<%= content_tag(:script, :id => 'entry-template', :type => 'text/x-handlebars-template') do %>
    ...
<% end %>

我没有看到使用content_tag这一点,但似乎过于复杂。

答案 1 :(得分:0)

尝试,

<%= javascript_tag :html_options => {id => 'entry-template', :type => 'text/x-handlebars-template'} do %>
    ------------------------
    ----------------------- 
<%end%>

愿它能奏效。我没试过。

相关问题