我有一个模型,分别用于汽车,卡车,公共汽车,货车等,我将它们列在自己的控制器和视图中:
- if @cars.present?
.pagination.justify-content-center
= will_paginate @cars
- else
= t "search.sorry_no_results"
- if @trucks.present?
.pagination.justify-content-center
= will_paginate @trucks
- else
= t "search.sorry_no_results"
以及公共汽车,货车等等。
我想把那个重复的块变成局部的。但是,我无法将@cars @trucks传递到@vehicles数组,以便所有这些模型可以共享该部分:
= render partial: "pagination", collection: @cars, as: :vehicles
- if vehicles.present?
.pagination.justify-content-center
= will_paginate vehicles
- else
= t "search.sorry_no_results"
由于我不想要逐项列出数组,我的简单解决方案只是传递所有数组:
.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"