如何使用 Ruby on Rails Mustache 从 ActiveModel 以 HTML 形式预览

发布于 2024-10-21 11:34:05 字数 1859 浏览 6 评论 0原文

我们正在使用 Mustache 模板,我想在结合模板的 RoR Web 应用程序中制作预览视图以及我们存储在数据库中的一些数据,但它并没有像我预期的那样工作,并且在互联网上进行一些搜索(包括SO!)后,我没有找到任何包含活动模型的示例。

如何将 ActiveModel 记录传送到 Mustache 以与模板合并?

设置:

架构 模型

create_table "templates", :force => true do |t|
  t.string   "kind"
  t.text     "data"
  t.integer  "data_count"
end
create_table "bars", :force => true do |t|
  t.string   "guid"
  t.string   "name"
  t.string   "summary"
end

没有什么特别之处。两者都是 ActiveRecord::Base 的子类。

class Bars < ActiveRecord::Base
end
class Templates < ActiveRecord::Base
end

控制器

class TemplateController < ApplicationController
  def preview
    @result = Mustache.render( template.data, :bars => Bar.limit(template.data_count ) ).html_safe
  end
end

视图

<%= @result %>

路由

get 'templates/:id/preview' => 'templates#preview', :as => 'templates_preview'

数据

y Bar.all

--- 
- !ruby/object:Bar
  attributes: 
    guid: "1"
    name: "test1"
- !ruby/object:Bar
  attributes: 
    guid: "2"
    name: "test2"

模板(出于示例目的,我简化了 html)

<html>
<head>
</head>
<body>
  {{#bars}}
    <a href="{{guid}}">{{name}}</a>
  {{/bars}}
</body>
</html>

结果

<html>
<head>
</head>
<body>
    <a href=""></a>
</body>
</html>

期望

<html>
<head>
</head>
<body>
    <a href="1">test1</a><a href="2">test2</a>
</body>
</html>

我希望对此有一个简单的答案,但我只是错过了它。谢谢。

We are using Mustache templates and I would like to make a preview View in our RoR web application that combines a template and some data we have stored in our database, but it does not work as I expected and after some searching on the internets (including SO!), I didn't find any examples that included an active model.

How do you pipe the ActiveModel records to Mustache to merge with a template?

The setup:

The Schema

create_table "templates", :force => true do |t|
  t.string   "kind"
  t.text     "data"
  t.integer  "data_count"
end
create_table "bars", :force => true do |t|
  t.string   "guid"
  t.string   "name"
  t.string   "summary"
end

There is nothing special about the models. Both are subclassed from ActiveRecord::Base

class Bars < ActiveRecord::Base
end
class Templates < ActiveRecord::Base
end

The Controller

class TemplateController < ApplicationController
  def preview
    @result = Mustache.render( template.data, :bars => Bar.limit(template.data_count ) ).html_safe
  end
end

The View

<%= @result %>

The Route

get 'templates/:id/preview' => 'templates#preview', :as => 'templates_preview'

The Data

y Bar.all

--- 
- !ruby/object:Bar
  attributes: 
    guid: "1"
    name: "test1"
- !ruby/object:Bar
  attributes: 
    guid: "2"
    name: "test2"

The Template (i've simplified the html for example purposes)

<html>
<head>
</head>
<body>
  {{#bars}}
    <a href="{{guid}}">{{name}}</a>
  {{/bars}}
</body>
</html>

The Result

<html>
<head>
</head>
<body>
    <a href=""></a>
</body>
</html>

The Expectation

<html>
<head>
</head>
<body>
    <a href="1">test1</a><a href="2">test2</a>
</body>
</html>

I am hoping there is an easy answer to this and I am just missing it. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-10-28 11:34:05

如果您将控制器更改为:(

@result = Mustache.render( template.data, :bars => Bar.limit(template.data_count).all ).html_safe

Bar.limit(template.data_count) 之后添加对 .all 的调用),

它会起作用吗?我对 Mustache 很陌生,但是快速浏览代码 似乎表明它为一个部分调用了这个:

v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator)

Bar.limit(template.data_count) 返回一个 ActiveRecord::Relation,它既不是 数组也不是枚举器。在关系上调用 .all 会将其转换为数组,并且应该导致 Mustache 将其相应地传递到该部分。

Does it work if you change your controller to:

@result = Mustache.render( template.data, :bars => Bar.limit(template.data_count).all ).html_safe

(added a call to .all after Bar.limit(template.data_count))

I'm pretty new to Mustache, but glancing very quickly through the code seems to show that it calls this for a section:

v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator)

Bar.limit(template.data_count) returns an ActiveRecord::Relation, which is neither an Array nor an Enumerator. Calling .all on the relation turns it into an array and should cause Mustache to pass it into the section accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文