1

如何在 Railstype中为助手添加自定义属性?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>
4

2 回答 2

2

javascript_tag是为,嗯,JavaScript,它的名字说得对。如果您想使用 a<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,只是似乎使事情过于复杂。

于 2013-09-10T22:52:46.983 回答
0

尝试,

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

愿它会奏效。我还没有尝试过。

于 2013-09-11T20:28:50.737 回答