ru3sch
ru3sch

Reputation: 796

Select and order values

I have a table of values, tasks, which I need to pull from a database, and then order as follows:

                <section class="entry-content">
                <header>    
                    <h4>Daily</h4>          
                </header>       

                <ul class="task-list" id="daily-tasks">
                    <% @daily.each do |task| %>
                        <% if task.status == "incomplete" %>                    
                            <li><% = task.title %></li>     
                        <% end %>
                    <% end %>

                    <% @daily.each do |task| %>
                        <% if task.status == "complete" %>                  
                            <li><% = task.title %></li>
                        <% end %>
                    <% end %>
                </ul>                           
            </section><!--.entry-content-->

I also have once, weekly, monthly, and potentially yearly tasks.

I'm new to Ruby, so I'm looking for the best way of doing this, as my current code isn't ideal

Upvotes: 0

Views: 55

Answers (1)

Jorge Najera T
Jorge Najera T

Reputation: 1551

I hope this can help you... I will suggest to use an application helper because you are trying to do the same task several times. I didnt test it, but its the complete idea.

Controller

@daily = Task.where("type = 'daily'")
@weekly =  Task.where("type = 'weekly'") 

View

<ul>
 <%= print_task (@daily,'completed') %> 
</ul>

<ul>
 <%= print_task (@daily,'incomplete') %> 
</ul>

<ul>
 <%= print_task (@weekly,'completed') %>           
</ul>

<ul>
 <%= print_task (@weekly,'incomplete') %> 
</ul>   
...

application_helper

def print_task(tasks, status)
 html =''
 tasks.each do |task|
   html += '<li>'
     if task.status == status
       html += "#{task.title}"
      end
   html += '</li>'
 end
 html.html_safe
end

Upvotes: 2

Related Questions