Ruby on rails 如何在show.html.erb中显示液体内容?
我正在rails 5中开发一个应用程序,我的目标是使用liquid在页面上显示产品 page.rbRuby on rails 如何在show.html.erb中显示液体内容?,ruby-on-rails,shopify,liquid,Ruby On Rails,Shopify,Liquid,我正在rails 5中开发一个应用程序,我的目标是使用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'
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(第页)
在显示页面上,仅显示“导轨中液体的测试”,但不显示产品。请帮我找出哪里出了问题,我对液体完全不熟悉
我在模型中使用了liquid方法,但是我得到了一个错误undefined method`liquid\u methods,所以我在stackoverflow上找到了定义为liquid的方法。我正在遵循一个教程该教程已经有10年历史了,最好还是坚持自述@phoet中的例子吧。我能够在rails c中使用@template=Liquid::template.parse(“hi{{{name}”)@template.render('name'=>'tobi')获得所需的结果,但上面描述的show.html.erb(页面)上所需的结果不起作用。我是否在页面模型中正确定义了to_liquid?这里只是猜测一下,但我认为
to_liquid
只是返回哈希值的任何方法,该哈希值反过来用于liquid呈现函数中的上下文。因此,我假设您必须呈现(@page.to\u liquid)
才能正确传递上下文
<%= RedCloth.new(Liquid::Template.parse(@page.content).render('page' => @page)).to_html %>
<%= 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 %>
Test for liquid in rails
{% for product in page.products %}
*{{ product.name }}* {{ product.price }}
Category: {{ product.category.name }}
{% endfor %}