2016-09-19 55 views
0

我的haml文檔中有四個循環用於測試目的,唯一的區別在於元素的順序。

我能以某種方式將邏輯放在我的控制器中,並在我的haml文檔中列出循環的整個'insidyness'一次?
目前我有一切重複4次,你知道,這感覺很糟糕。 :)Rails 4:如何在控制器內部放置循環邏輯

例如:

- @loop.where(featured: true).order("RANDOM()").limit(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.order(:cached_votes_up => :desc).limit(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.order(:impressions_count => :desc).limit(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.order("created_at DESC").limit(4).each do |item| 
    %li 
     %h1 This is an example 

控制器:

def index 
    @loop = Item.all 
end 

我想減少我的Haml的代碼,以這樣的事情,並在控制器的移動休息:

- @loop.each do |item| 
    %li 
     %h1 This is an example 

在此先感謝您的每一個答案!在Item模型類

+0

什麼是@循環?你到底在做什麼?如果不瞭解問題,我們無法幫助您。 – Surya

+0

對不起,我添加了這個答案:) – Gugubaight

+0

顯示你的期望。 –

回答

2

您無法多次渲染該視圖,但可以執行此類操作。

def index 
    @loops = [ 
    Item.where(featured: true).order("RANDOM()"), 
    Item.order(:cached_votes_up => :desc), 
    Item.order(:impressions_count => :desc), 
    Item.order("created_at DESC") 
    ] 
end 

然後在模板

- @loops.each do |loop| 
- loop.limit(4).each do |item| 
    %li 
    %h1 This is an example 
+0

關於此問題只有一個問題:這是否會以不同的順序渲染四次(如預期的那樣),還是將所有內容合併到一個循環中? – Gugubaight

+0

這是兩個循環。第一個循環調用第二個循環4次(每次以不同順序的項目)。第二個循環呈現該循環中每個項目的html。 –

+0

你在哪裏啓動'loop'循環? – Gugubaight

2

scope :featured_with_random_order_desc, ->(limit_value) { where(featured: true).order("RANDOM()").limit(limit_value) } 

scope :by_cached_votes_order_desc, ->(limit_value) { order(:cached_votes_up => :desc).limit(limit_value) } 

scope :by_impression_count_order_desc, ->(limit_value) { order(:impressions_count => :desc).limit(limit_value) } 

scope :by_created_at_desc, ->(limit_value) { order("created_at DESC").limit(limit_value) } 

在您的觀點:

- @loop.featured_with_random_order_desc(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.by_cached_votes_order_desc(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.by_impression_count_order_desclimit(4).each do |item| 
    %li 
     %h1 This is an example 
- @loop.by_created_at_desc.limit(4).each do |item| 
    %li 
     %h1 This is an example 

你可以走了一步,並進一步爲在控制器每次循環創建變量:

def index 
    @loop_featured = Item.featured_with_random_order_desc(4) 
    @loop_cached_votes = Item.by_cached_votes_order_desc(4) 
    @loop_impression_counts = Item.by_impression_count_order_desclimit(4) 
    @loop_by_created = Item.by_created_at_desc(4) 
end 

並在視圖中使用它們:

- @loop_featured.each do |item| 
    %li 
    %h1 This is an example 
- @loop_cached_votes.each do |item| 
    %li 
    %h1 This is an example 
- @loop_impression_counts.each do |item| 
    %li 
    %h1 This is an example 
- @loop_by_created.each do |item| 
    %li 
    %h1 This is an example 
0

您可以使用union_scopeActiveRecord

#item.rb 

    include ActiveRecord::UnionScope 
    scope :by_random, -> { where(featured: true).order("RANDOM()").limit(4) } 
    scope :by_cached_votes, ->{ order(:cached_votes_up => :desc).limit(4) } 
    scope :by_impression_count, ->{ order(:impressions_count => :desc).limit(4) } 
    scope :by_created_at, ->{ order("created_at DESC").limit(4) } 

    scope: all_conditions, -> { union_scope(by_random, by_cached_votes,by_impression_count,by_created_at} 

控制器

@item = Item.all_conditions 

您的看法:

- @loop.all_conditions.each do |item|