1
votes

I wrote an app for shopify which works as designed. Now I wanted to add the app proxy to make the content embedded in the shop. The proxy is working fine but shopify expects a response (in my case a view) as pure liquid. The original view in ruby has some logic using several variables and objects.

I installed the liquid gem. The tutorials I walked through seem to be a little bit outdated.

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

This is working, but it only works with strings. I watched the railscast which I thought will be an easy way to allow liquid to access the objects. But again its outdated and the "liquid_methods" isn't working any more.

So i wrote my own method in my model:

  def to_liquid
    { "title" => self.title,
      "description" => self.description
    } 

If i try to render it with my method in my console like this:

@template = Liquid::Template.parse("Welcome to {{object.title}}")
@template.render(object.title.to_liquid)

I get the following error:

Liquid::ArgumentError (Liquid error: Expected Hash or Liquid::Context as parameter)

Now Im stuck...

What is the most simple way to access RoR Objects with liquid?

1

1 Answers

1
votes

After trying almost every possible way to pass the variable, this did the trick:

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