将rails变量传递给液体可以在控制台中工作,但不能在视图中工作 [英] Passing rails variable to liquid works in console but not in view

查看:62
本文介绍了将rails变量传递给液体可以在控制台中工作,但不能在视图中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将哈希传递给render方法.当我这样做时,在控制台中一切正常:

I want to pass a hash to the render method. In my console everything works fine when I do this:

@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"])

但是,当我想通过控制器呈现页面并将其传递给期望香草液体的app-proxy时,则不会显示哈希键.但这是解释性的,因为视图显示空白.如果根本不起作用,则该视图将显示:欢迎使用{{object_title}}".甚至由于语法错误甚至无法加载.

But when i want to render the page through my controller and pass it through an app-proxy which expects vanilla liquid, the hash-key isn't appearing. But it's interpreted, because the view shows blank space. If it wouldn't work at all, the view would show:"Welcome to {{object_title}}" or don't even load because of syntax errors.

我尝试了几乎所有可能的方式来渲染模板.接下来的两个试用版不会引发错误,但仅在标题应显示的地方显示空白:

I tried almost every possible way to render the template. The next two tryouts are the ones which do not throw an error, but show only a blank where the title should appear:

@pageview = Liquid::Template.parse(File.read(Rails.root + "app/views/app_proxy/show.html.erb"))
render text: @pageview.render('object_title' => @hash_object["title"]), content_type: "application/liquid"

第二个(我认为它更干净,更像红宝石):

And the second one (which I think is a bit cleaner and more rubylike):

    respond_to do |format|
      format.html do
      render text: @pageview.render('object_title' => @hash_object["title"]), layout: false,  content_type: "application/liquid"
      end
    end

这些渲染图中的错误在哪里?或者我想念什么?

Where is the mistake in these renderings or what am I missing?

推荐答案

@变量被分配给类的实例,而不是类本身,因此您需要将其声明为"initialize"(初始化).方法(如果是测试,则为设置"方法)

@ variables are assigned to an instance of a class, not the class itself, so you need to declare it an "initialize" method (or "setup" method, if a test)

这篇关于将rails变量传递给液体可以在控制台中工作,但不能在视图中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆