質問

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