문제

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