0

関連のない 2 つのテーブルがあり、それらを同時にループして、それぞれの異なる属性を呼び出す必要があります。したがって、現時点では(簡単にするために一般的な名前を使用しています):

<% @combined = Tableone.all + Tabletwo.all %>
<% @combined.each do |c| %>
<% if c.is_a?(Tableone) %> 
     do some code....
<% elsif c.is_a?(Tabletwo) %>
     do other code...
<% end %>
<% end %>

そのため、現時点では elsif ブロックに到達しておらず、Tableone の結果のみが表示されています。これを回避して両方のテーブルをループするにはどうすればよいですか?

編集: これが完全なコードです。

<% @subcategory = Subcategory.all %>
<% @product = Product.all %>
<% @ps = Product.all + Subcategory.all %>

<% @ps.each do |ps| %>

    <% if (ps).is_a?(Product) %>
    <% if (ps).display_on_home_page and !(ps).is_highlight_product and !(ps == '..') %>
        <% if ps.price_from %>
            <div class="column_entry">
           <div class="product_special">
            <span class="a">From Only</span>
            <span class="b"><%= number_to_currency(ps.price,:unit=>'€') %></span></div>
                <%= link_to image_tag(ps.product_image.url(:normal_page_size)), products_content_url(ps.id), :controller=>'products' %>
            </div>
         <% else %>
            <div class="column_entry">
            <div class="product_special">
            <span class="a">Only</span>
            <span class="b"><%= number_to_currency(ps.price,:unit=>'€') %></span>
            </div>
            <%= link_to image_tag(ps.product_image.url(:normal_page_size)), products_content_url(ps.id), :controller=>'products' %>
            </div>
            <% end %>
    <% elsif (ps).is_a?(Subcategory) %>
        <%= "Reached elsif" %>
        <% if (ps).display_on_home_page and !(ps).is_highlight_product and !(ps == '..') %>
        <div class="column_entry">
            <%= link_to image_tag(ps.image_attachment.url(:normal_page_size)), subcategories_content_url(ps.id), :controller=>'subcategories' %>
        </div>                          
    <% end %>                                                           
    <% end %>

<% end %>
<% end %>
4

1 に答える 1

0

So, finally got it figured out. The problem was that @ps was being defined in the HTML. Defining @ps in the controller like so:

@ps = Products.where(:display_on_home_page=>true) + Subcategory.where(:display_on_home_page=>true)

means that the loop will see both products and subcategories. I'm not entirely sure why this is other than, by convention, the variable declaration should have been in the controller.

于 2013-11-12T16:02:14.157 に答える