如何使用 Ruby on Rails Mustache 从 ActiveModel 以 HTML 形式预览
我们正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将控制器更改为:(
在
Bar.limit(template.data_count)
之后添加对.all
的调用),它会起作用吗?我对 Mustache 很陌生,但是快速浏览代码 似乎表明它为一个部分调用了这个:
Bar.limit(template.data_count)
返回一个ActiveRecord::Relation
,它既不是数组
也不是枚举器
。在关系上调用.all
会将其转换为数组,并且应该导致 Mustache 将其相应地传递到该部分。Does it work if you change your controller to:
(added a call to
.all
afterBar.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:
Bar.limit(template.data_count)
returns anActiveRecord::Relation
, which is neither anArray
nor anEnumerator
. Calling.all
on the relation turns it into an array and should cause Mustache to pass it into the section accordingly.