Using liquid in Rails 3

女生的网名这么多〃 提交于 2019-12-24 05:30:46

问题


Im making a Rails blog engine for learning purpose. I want to use liquid as template engine. I have something like this

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

That gave me the following error:

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

I already had LiquidView loaded in initializers/liquid.rb Please let me know what is my problem. Thank you


回答1:


As I know you should have liquid methods for attributes (in your case for 'title'). try something like this

class Post < ActiveRecord::Base
  liquid_methods :title
end

and see.

If not try to make Post class inherited by Liquid::Drop

like

class Posts < Liquid::Drop

end

** BTW since you get an error claiming missing template variable make sure your liquid rendering part is as follows

(directly copied from liquid doc)

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

hope this helps

cheers

sameera



来源:https://stackoverflow.com/questions/5453639/using-liquid-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!