如何将数组传递给具有其他名称的部分

时间:2018-01-19 20:44:55

标签: ruby-on-rails

我有一个模型,分别用于汽车,卡车,公共汽车,货车等,我将它们列在自己的控制器和视图中:

汽车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"

以及公共汽车,货车等等。

我想把那个重复的块变成局部的。但是,我无法将@cars @trucks传递到@vehicles数组,以便所有这些模型可以共享该部分:

汽车指数

= 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"

我的解决方案

由于我想要逐项列出数组,我的简单解决方案只是传递所有数组:

_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"

0 个答案:

没有答案