在Rails中使用液体3

Voi*_*idx 2 templates ruby-on-rails liquid

我正在制作一个Rails博客引擎用于学习目的.我想用液体作为模板引擎.我有类似的东西

    ## posts_controller.rb
    ...
    def index
      @posts = Post.all
    end
   ... 
    ## posts/index.html.liquid
    {% for post in posts do %}
      {{ post.title }}
    {% endfor %}
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

undefined local variable or method `template' for
#<PostsController:0x103d16290>
Run Code Online (Sandbox Code Playgroud)

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

sam*_*207 5

据我所知,你应该有属性的液体方法(在你的情况下为'标题').尝试这样的事情

class Post < ActiveRecord::Base
  liquid_methods :title
end
Run Code Online (Sandbox Code Playgroud)

并看到.

如果没有尝试使Liquid :: Drop继承Post类

喜欢

class Posts < Liquid::Drop

end
Run Code Online (Sandbox Code Playgroud)

**BTW,因为您收到错误声明缺少模板变量,请确保您的液体渲染部分如下所示

(直接从液体doc复制)

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

希望这可以帮助

干杯

sameera