Passing rails variable to liquid works in console but not in view

Viewed 563

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

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?

2 Answers

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

render html: instead of render text:

Related