2

我正在制作一个 Rails 博客引擎用于学习目的。我想使用液体作为模板引擎。我有这样的东西

    ## posts_controller.rb
    ...
    def index
      @posts = Post.all
    end
   ... 
    ## posts/index.html.liquid
    {% for post in posts do %}
      {{ post.title }}
    {% endfor %}

这给了我以下错误:

undefined local variable or method `template' for
#<PostsController:0x103d16290>

我已经在 initializers/liquid.rb 中加载了 LiquidView 请让我知道我的问题是什么。谢谢

4

1 回答 1

5

据我所知,您应该对属性有流动的方法(在您的情况下是“标题”)。尝试这样的事情

class Post < ActiveRecord::Base
  liquid_methods :title
end

看看。

如果不尝试让 Liquid::Drop 继承 Post 类

喜欢

class Posts < Liquid::Drop

end

** 顺便说一句,因为您收到错误声明缺少模板变量,请确保您的液体渲染部分如下

(直接从liquid doc复制)

@template = Liquid::Template.parse("hi {{name}}")  # Parses and compiles the template
@template.render( 'name' => 'tobi' )               # Renders the output => "hi tobi"

希望这可以帮助

干杯

同龄人

于 2011-03-28T03:59:59.053 回答