我正在尝试创建一个haml模板,该模板使用我的ruby应用程序中的一些数据来填充一些内容.是否可以将参数传递给haml以使其正确呈现?这是我如何获取我的haml模板并呈现它:
template = File.open('path/to/template.haml')
html = Haml::Engine.new(template.read).render
Run Code Online (Sandbox Code Playgroud)
那么,是否可以将对象从本地Ruby脚本传递到模板文件中,以便页面正确呈现?或者,我可以将haml文件拉入对象吗?
如果这不起作用,我唯一的另一个想法是将模板构建为本地字符串,这对我来说似乎更乏味.那么,是否有一种不同的编码模式对这项工作更有效?
我正在格式化菜单,以便在您点击它之后标题的颜色不同.我想为每个视图使用相同的haml模板,并希望控制器更改特定html元素的类.如何从控制器中定位特定的html元素并为其添加一个类?
这是一个示例:
哈姆尔:
%tr
%th
%a#name-header= link_to "Name", people_path({:sort => 'by_name'})
%th Date
%th Description
%th More Info
Run Code Online (Sandbox Code Playgroud)
控制者:
def index
case params[:sort]
when "by_name"
@people = Person.find(:all, :order => "name")
#How can I change the class of the th element here
else
@people = Person.all
end
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在设置value集合中的所有模型的属性(在_.each循环中).循环结束后,每个value属性都保持不变.我相信这是因为_.each循环为每个模型创建一个新的实例变量.有没有标准的方法来更新我没有关注的集合模型?
该代码应该强制骰子成直(1,2,3,4,5,6).rollableDice是指具有value属性的Backbone模型的Backbone集合.这是容器模型上的方法:
makeStraight: function() {
console.log('making straight');
console.log(_(this.rollableDice.models).pluck('value'));
var counter = 1;
_(this.rollableDice.models).each(function(die) {
console.log(die.get('value'));
die.set('value', counter);
console.log(die.get('value'));
counter++;
});
console.log(_(this.rollableDice.models).pluck('value'));
},
Run Code Online (Sandbox Code Playgroud)
这是我在控制台上看到的输出:
making straight
[1, 1, 3, 5, 2, 1]
undefined
1
undefined
2
undefined
3
undefined
4
undefined
5
undefined
6
[1, 1, 3, 5, 2, 1]
Run Code Online (Sandbox Code Playgroud)
//编辑这是我希望看到的控制台输出:
making straight
[1, 1, 3, 5, 2, 1]
1
1
1
2
3
3
5
4
2
5 …Run Code Online (Sandbox Code Playgroud)