问题描述
我为shopify编写了一个应用程序,该应用程序可以按设计工作。现在,我想添加应用程序代理以使内容嵌入商店。代理工作正常,但是shopify希望以纯净液体的形式做出回应(在我的情况下为观点)。红宝石的原始视图具有使用多个变量和对象的某种逻辑。
我安装了液体宝石。我浏览的教程似乎有些过时了。
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render( 'name' => 'tobi' ) # Renders the output
=> "hi tobi"
这是有效的,但仅适用于字符串。我看过railscast,我认为这是允许液体进入物体的简便方法。但是,它又过时了,“ liquid_methods”不再起作用了。
所以我在模型中编写了自己的方法:
def to_liquid
{ "title" => self.title,"description" => self.description
}
如果我尝试使用控制台中的方法呈现它,如下所示:
@template = Liquid::Template.parse("Welcome to {{object.title}}")
@template.render(object.title.to_liquid)
我收到以下错误:
Liquid::ArgumentError (Liquid error: Expected Hash or Liquid::Context as parameter)
现在我卡住了...
用液体访问RoR对象的最简单方法是什么?
解决方法
尝试了几乎所有可能的方式传递变量后,就成功了:
@object = Object.find(params[:id])
@hash_object = @object.to_liquid
@template = Liquid::Template.parse("Welcome to {{object_title}}")
@template.render('object_title' => @hash_object["title"])