I am working on an app in rails 5 and my goal is to displays products on pages show page using liquid.
page.rb
def to_liquid
Product.all
end
product.rb
belongs_to :category
def to_liquid
{ 'name' => self.name,
'price' => self.price,
'description' => self.description,
'category' => self.category
}
end
category.rb
has_many :products
def to_liquid
{ 'name' => self.name}
end
show.html.erb (Page)
<%= RedCloth.new(Liquid::Template.parse(@page.content).render('page' => @page)).to_html %>
_form.html.erb(page)
<%= form_with(model: page, local: true) do |form| %>
<% if page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
In the text area for the content if I add the below text
Test for liquid in rails
{% for product in page.products %}
*{{ product.name }}* {{ product.price }}
Category: {{ product.category.name }}
{% endfor %}
on the show page only "Test for liquid in rails" appears but the products don't appear. Please help me figure out where I am going wrong I am completely new to liquid.
Instead of defining to_liquid I was using liquid_methods in the model but I get an error undefined method `liquid_methods so I found on stackoverflow to define to_liquid. I am following a tutorial http://railscasts.com/episodes/118-liquid?autoplay=true