0

I have one model each for Cars, Trucks, Buses, Vans, etc and I list them in their own controller and view:

Cars index.html.slim

- if @cars.present?
  .pagination.justify-content-center
    = will_paginate @cars
  - else
    = t "search.sorry_no_results"

Trucks index.html.slim

- if @trucks.present?
  .pagination.justify-content-center
    = will_paginate @trucks
  - else
    = t "search.sorry_no_results"

And so on with Buses, Vans, etc.

I want to turn that repetitive chunk into a partial. However, I'm unable to pass the @cars @trucks into a @vehicles array so that all those models can share that partial:

Cars index

= render partial: "pagination", collection: @cars, as: :vehicles

_pagination.html.slim

- if vehicles.present?
  .pagination.justify-content-center
    = will_paginate vehicles
- else
  = t "search.sorry_no_results"

My Solution

Since I do not want to itemize the array, my simple solution is just to pass all the arrays:

_pagination.html.slim

.pagination.justify-content-center
  - if @cars.present?
    = will_paginate @cars
  - elsif @trucks.present?
    = will_paginate @trucks
  - elsif @buses.present?
    = will_paginate @buses
  - elsif @vans.present?
    = will_paginate @vans
  - elsif @...
    ...
  - else
    = t "search.sorry_no_results"
Jun Dalisay
  • 1,165
  • 2
  • 12
  • 26

0 Answers0