2
votes

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
When you inspect the class @hash_object["title"] does it return String or something else? It might be just the case that you tell it @hash_object["title"].to_s. I haven't worked with Liquid though, but if for some reason it doesn't read your value from the key, Ruby might have interpreted it as something else.mutantkeyboard
The hash method is defined in my model and i checked the type with is_a?(String) which gives me a "true". Nevertheless i added it and deployed it with the same result.Spinshot
Ok. I also noticed that you try to render the .erb file inside the Liquid and then use it to render text. But my question is: What happens if you render raw file instead, so instead render text place render file? I'm really not used too much to other templating engines apart from HAML and ERB, so this might not be the best advice ever.mutantkeyboard
tried it right now as render file with locals. Same resultSpinshot
sorry, my answer doesn't apply here. I tried another answer. I'll delete this oneChris

2 Answers

1
votes

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

0
votes

render html: instead of render text: