I have four loops in my haml document for testing purposes with the only difference being the order of the elements.
Am I somehow able to put the logic in my controller and list the whole 'insidyness' of the loop only once in my haml document?
Currently I have everything duplicated 4 times and ya know, that feels bad. :)
For instance:
- @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
Controller:
def index
@loop = Item.all
end
I would like to reduce my Haml Code to something like this and move the rest in the Controller:
- @loop.each do |item|
%li
%h1 This is an example
Thanks in advance for each answer!
@loop? what are you exactly trying to do? without understanding the problem we can not help you.