我正在尝试从属于特定类别的产品列表中显示单个图像。例如我希望展示一种食品,一种家居用品,一种服装产品等...... 每个产品都有一个字段,其中填入一个下拉菜单,指示它属于哪个类别。 我在控制器中实现了以下逻辑
if Product.where(:department.eql? "Stationery") then
@stationery = [Product.where(:department.eql? "Stationery").first]
end
if Product.where(:department.eql? "Food") then
@food = [Product.where(:department.eql? "Food").first]
end
if Product.where(:department.eql? "Toiletries") then
@toiletries = [Product.where(:department.eql? "Toiletries").first]
end
if Product.where(:department.eql? "Household") then
@household = [Product.where(:department.eql? "Household").first]
end
if Product.where(:department.eql? "Clothing") then
@clothing = [Product.where(:department.eql? "Clothing").first]
end
if Product.where(:department.eql? "Accessories") then
@accessories = [Product.where(:department.eql? "Accessories").first]
end
并且在视图中我有这个
<% @stationery.each do |stationery| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Stationery') %>
<%= link_to image_tag(stationery.product_image.url(:normal_page_size)), products_content_url(stationery.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
<% @food.each do |food| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Food') %>
<%= link_to image_tag(food.product_image.url(:normal_page_size)), products_content_url(food.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
<% @toiletries.each do |toiletries| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Toiletries') %>
<%= link_to image_tag(toiletries.product_image.url(:normal_page_size)), products_content_url(toiletries.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
<% @household.each do |household| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Household') %>
<%= link_to image_tag(household.product_image.url(:normal_page_size)), products_content_url(household.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
<% @clothing.each do |clothing| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Clothing') %>
<%= link_to image_tag(clothing.product_image.url(:normal_page_size)), products_content_url(clothing.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
<% @accessories.each do |accessories| %>
<div class="column_entry">
<% if Product.where(:department.eql? 'Accessories') %>
<%= link_to image_tag(accessories.product_image.url(:normal_page_size)), products_content_url(accessories.id), :controller=>'products' %>
<% end %>
</div>
<% end %>
我遇到的问题是有6种产品进入,但它们都是相同的产品,我的逻辑一定存在问题我无法看到。
我知道我通过控制器和视图在这里加倍了一段时间,但是我已经把这段代码搞砸了一段时间并尝试了很多变化。
我的猜测就是这一行
if Product.where(:department.eql? 'Household')
和类似的行,但我无法弄清楚如何使这个工作。 非常感谢任何帮助
答案 0 :(得分:1)
你做的太多“如果”,你真的需要那么多吗?这使你的代码难以调试。
尝试类似
的内容@products = {}
["Stationery","Food","Toiletries","Household","Clothing","Accessories"].each do |dep|
@products[dep] = Product.where(department: dep).first
end
并将所有观点替换为:
<%- ["Stationery","Food","Toiletries","Household","Clothing","Accessories"].each do |dep| -%>
<div class="column_entry">
<%- if product = @products[dep] -%>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product), :controller=>'products' -%>
<%- end -%>
</div>
<%- end -%>
编辑:稍微更改了代码,你甚至可以让那个deparments数组保持恒定