Question

Can someone suggest me a nice little way to return multiple things in a method and render all of them as different partials in the view?

def tasks
   if task1.present?
     ['task1_summary', task1: task1]
   if task2.present?
     ['task2_programs', task2: task2]
   if task3.present?
     ['task3', program: task3]
   else
     [empty: '']
   end
  end

In my view:

  = render *tasks

The above code would just render one partial.

I am trying to figure out a way that would return all the partials if all the above conditions are true

Was it helpful?

Solution

def tasks
tasks_array = []

   tasks_array <<  ['task1_summary', task1: task1] if task1.present?

   tasks_array << ['task2_programs', task2: task2] if task2.present?

   tasks_array << ['task3', program: task3] if task3.present?

end

would return an array of your tasks. You can then go through them with .each in your view.

OTHER TIPS

Try to return an array of arguments which you then pass to the render call:

In Erb:

<% tasks.each do |task| %>
  <%= render task %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top