【问题标题】:How do I call Coffeescript templates in a Javascript ERB template in a jquery_ujs response?如何在 jquery_ujs 响应中的 Javascript ERB 模板中调用 Coffeescript 模板?
【发布时间】:2011-11-28 19:21:59
【问题描述】:

我在我的 Rails 3.1 应用程序中使用 Coffeescript template,所以在我的 app/assets/javascripts/post.js.coffee 文件中,我有类似的内容:

$('#post').html JST['templates/posts/show'] post: post

其中 post 是一个 JSON 对象。模板位于 app/assets/javascripts/templates/posts/show

现在我也在使用 jquery_ujs 来响应 PostsController#create 并想使用 app/views/posts/show.js.erb 来呈现响应。在 show.js.erb 中,我想使用上面的模板。我该怎么做呢?谢谢。

【问题讨论】:

    标签: jquery ruby-on-rails coffeescript ujs


    【解决方案1】:

    似乎https://github.com/markbates/coffeebeans 可能就是您要找的东西。

    【讨论】:

      【解决方案2】:

      使用 Rails 模板的强大功能。 如果您将您的响应文件重命名为 show.js.coffee.erb(或 show.coffee.erb - 不记得),那么 Rails 模板引擎会为您生成 .erb 代码,之后它会将咖啡转换为 js,并且仅之后,您的客户将获得响应文本。

      使用 .erb 可以正常工作。使用 .haml 我有一些问题...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        • 2012-04-16
        • 2014-12-12
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 2011-03-26
        相关资源
        最近更新 更多