如何在show.html.erb中显示液体含量?

时间:2018-11-05 19:35:32

标签: ruby-on-rails shopify liquid

我正在使用Rails 5开发一个应用程序,我的目标是使用液体在页面显示页面上显示产品。

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(页面)

<%= RedCloth.new(Liquid::Template.parse(@page.content).render('page' => @page)).to_html %>

_form.html.erb(页面)

<%= 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 %}

在显示页面上仅出现“测试导轨中的液体”,但没有出现产品。请帮我弄清楚哪里出问题了。我完全不熟悉液体。

我没有在模型中使用liquid_method来定义to_liquid,但是却得到了一个未定义的错误方法`liquid_methods,因此我在stackoverflow上发现了定义to_liquid的方法。我正在关注教程http://railscasts.com/episodes/118-liquid?autoplay=true

0 个答案:

没有答案
相关问题