【问题标题】:'if' statement logic with a 'where' condition带有“where”条件的“if”语句逻辑
【发布时间】: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


    【解决方案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 数组设置一个常量

    【讨论】:

    • 这给了我undefined method product_image,因为我没有@products.each do |products|
    • 你在哪里使用@products.each?我在提交后编辑了我的代码,如果您需要循环 @products 哈希,请执行“@products.each do |dep, product|”之类的操作因为它是一个哈希,而不是一个数组
    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多