【问题标题】:Rails, Backbone, Handlebars template and .erbRails、Backbone、Handlebars 模板和 .erb
【发布时间】:2014-03-11 17:39:02
【问题描述】:

我正在构建一个使用 Backbone 作为前端框架的 Rails 应用程序。我正在使用 Handlebars 作为模板。

在 Rails 方面,我有一个出版物模型,每个出版物都有很多文章。现在我想将我的出版物名称列表打印到屏幕上。

当我将我的出版物集合传递给视图的渲染函数时,我可以使用此代码为我的车把模板很好地显示名称:

{{#each publications}}
  {{#if name}}
    <li style='color:green'>
      {{name}}
    </li>
  {{/if}}
{{/each}}

现在我还想要在名称按钮旁边有一个允许我编辑和删除的按钮,就像在 Rails 最初生成的视图 >publications>index.html.erb 页面中一样。代码是:

      <%= link_to t('.edit', :default => t("helpers.links.edit")),
                  edit_publication_path(publication), :class => 'btn btn-mini' %>
      <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                  publication_path(publication),
                  :method => :delete,
                  :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                  :class => 'btn btn-mini btn-danger' %>

由于此代码涉及 .erb 标签,我不知道如何将其集成到我的车把模板中。有人可以帮我解决这个问题吗?我想要完成的工作是否存在根本性错误?

【问题讨论】:

    标签: ruby-on-rails templates backbone.js handlebars.js erb


    【解决方案1】:

    您应该能够将车把模板文件重命名为 *.hbs.erb 并像往常一样在其中使用 erb 标签。

    Rails 将按照文件扩展名从右到左的顺序编译文件。 在这种情况下,erb 内容将被注入到车把文件中,然后将 预编译服务器端,或发送到客户端并在运行时编译(默认行为)。

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 2014-06-05
      • 2014-06-01
      • 1970-01-01
      • 2012-07-10
      • 2012-04-10
      • 1970-01-01
      • 2023-03-27
      • 2017-07-31
      相关资源
      最近更新 更多