Home > Software engineering >  Using same partial but with different variable for loop Rails
Using same partial but with different variable for loop Rails

Time:11-07

I wan't to use the same partial, but changing the main variable of the loop inside the partial so that the layout is the same, but different results show in each tab. I thought I could make this happen using locals, but I tried it with no luck. ¿Any ideas?

<div  id="pills-tabContent">
  <div  id="pills-home" role="tabpanel" aria-labelledby="campaigns-active-tab">
    <%= render partial: 'campaigns_list' %>
  </div>
  <div  id="pills-profile" role="tabpanel" aria-labelledby="campaigns-inactive-tab">
    <%= render partial: 'campaigns_list' %>
  </div>
</div>

Controller:

@active_campaigns = @campaigns.where(status: 1).order(created_at: :desc)
@inactive_campaigns = @campaigns.where(status: 2).order(created_at: :desc)

Partial:

Here, the variable should change from active to inactive campaigns:

  <% @campaigns.each do |campaign| %>
      <tr scope="row d-flex align-items-center justify-content-center">

CodePudding user response:

I thought I could make this happen with locals

Yes you can; however, the code you provided does not use locals.

Local usage would be:

<%= render partial: 'campaigns_list', locals: { campaigns: @active_campaigns } %>

And you can thus loop through campaigns within the partial.

Docs for locals

  • Related