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

有帮助吗?

解决方案

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.

其他提示

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

In Erb:

<% tasks.each do |task| %>
  <%= render task %>
<% end %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top