【发布时间】:2013-11-19 11:40:08
【问题描述】:
我正在尝试显示属于特定类别的产品列表中的单个图像。例如,我希望展示一种食品、一种家居用品、一种服装产品等... 每个产品都有一个用下拉菜单填写的字段,该菜单指示它所属的类别。 我在控制器中实现了以下逻辑
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')
和类似的行,但我不知道如何让它工作。 非常感谢任何帮助
【问题讨论】:
标签: ruby-on-rails loops if-statement where