简体   繁体   English

在 Ruby on Rails 中从视图中删除逻辑

[英]Removing logic from view in Ruby on Rails

I have an index view that iterates through a list of products with specific state_taxonomies.我有一个索引视图,它遍历具有特定 state_taxonomies 的产品列表。 I was able to get the logic to work with a query request within the view, but I'm assuming <% Product.where(id: st.product_id).each do |tax| %>我能够获得在视图中处理查询请求的逻辑,但我假设<% Product.where(id: st.product_id).each do |tax| %> <% Product.where(id: st.product_id).each do |tax| %> logic should not display within a view? <% Product.where(id: st.product_id).each do |tax| %>逻辑不应显示在视图中?

  1. Where should I put this logic?我应该把这个逻辑放在哪里?
  2. How should I create this method?我应该如何创建这个方法? I've tried a few routes but failed.我尝试了几条路线,但都失败了。

Products index view user Bootstrap nav-pills:产品索引查看用户 Bootstrap nav-pills:

<div>
  <% @state_taxonomies.each do |st| %>
    <div class="tab-pane" id="<%= st.id %>">
      <% Product.where(id: st.product_id).each do |tax| %>
        <%= link_to tax.title, tax %>
      <% end %>
    </div>
  <% end %>
</div>

Products Controller:产品控制器:

def index
  @products = Product.all.page params[:page]
  @state_taxonomies = StateTaxonomy.all 
end

Note: products has_many :state_taxonomies and state_taxonomy belongs_to :product.注意:产品 has_many :state_taxonomies 和 state_taxonomy Beings_to :product。

You are doing a lot of queries, first in your controller Product.all (you don't seem to use the result) and StateTaxonomy.all .您正在执行大量查询,首先是在您的控制器Product.all (您似乎没有使用结果)和StateTaxonomy.all But the worse is in your view, you do a Product.where(id: st.product_id) for each @state_taxonomies .但更糟糕的是,在你看来,你为每个@state_taxonomies做一个Product.where(id: st.product_id)

You should research the n+1 problem and eager loading .您应该研究 n+1 问题和预先加载 Also, never do a query in a view, that goes in the controller.此外,永远不要在视图中执行查询,该查询在控制器中进行。

All you need is to include the products when you query for StateTaxonomy in your controller :您只需要在控制器中查询 StateTaxonomy 时包含产品:

@state_taxonomies = StateTaxonomy.all.includes(:product)

Then in your view :那么在你看来:

<% @state_taxonomies.each do |st| %>
  <div class="tab-pane" id="<%= st.id %>">
    <% st.products.each do |tax| %>
      <%= link_to tax.title, tax %>
    <% end %>
  </div>
<% end %>

Edit : I just realized the view can't work this way.编辑:我刚刚意识到视图不能以这种方式工作。 In your models, a state_taxonomy belongs to a product, so it can only have one product.在您的模型中,state_taxonomy 属于一个产品,因此它只能有一个产品。 I don't know if that is what you want or if it's an error.我不知道这是你想要的还是错误。

You can either only show one product per state_taxonomy :每个 state_taxonomy 只能显示一种产品:

<% @state_taxonomies.each do |st| %>
  <div class="tab-pane" id="<%= st.id %>">
    <%= link_to st.product.title, st.product %>
  </div>
<% end %>

or change the relation between the two models, maybe you want has_and_belongs_to_many .或者改变两个模型之间的关系,也许你想要has_and_belongs_to_many

Your Products controller should be something like:您的产品控制器应该是这样的:

def index
  @state_taxonomies = StateTaxonomy.all
  @products = Hash.new
  @state_taxonomies.each |st| do 
    @products[st.id] == Product.where(id: st.product_id)
  end
end

This would store in @products all the required in your view and you could write only:这会将您视图中所需的所有内容存储在@products 中,您只能编写:

<% @state_taxonomies.each do |st| %>
  <div class="tab-pane" id="<%= st.id %>">
    <% @products[st.id].each do |tax| %>
      <%= link_to tax.title, tax %>
    <% end %>
  </div>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM