[英]Sort Date (Newest to Oldest) ROR
這是 index.html.erb
<%= link_to_modal_new(new_master_film_path, "Enter film") %>
<%= link_to_export("Master films", params) %>
<%= paginate @master_films %>
<table class="table table-condensed table-hover">
<thead>
<tr>
<th></th>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= render @master_films %>
</tbody>
</table>
我需要在 desc(最新到最舊)日期中組織 Date 列。 我添加了以下下拉選項,但失敗了。 錯誤模板。 或者,如果我可以默認排序為最新的層壓日期是第一個。
<%= render 'shared/sort_dropdown', current: sort, choices: [['serial','desc'], ['laminated','desc']] %>
我正在詳細寫這篇文章,假設您對 Rails 非常陌生,因為您提出問題的方式和您的視圖代碼的方式。 我的道歉,如果我錯了。
在您的控制器(我猜它應該是films_controller.rb
或master_films_controller.rb
)中,您正在計算此@master_films
(它將與具有您發布的代碼的視圖具有相同的名稱),添加以下內容:
@master_films = MasterFilm.all.order('laminated DESC')
# OR
@master_films = MasterFilm.all.order(laminated: 'desc')
# letter case does not matter
您在視圖中的表代碼不正確。 它應該是:
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= @master_films.each do |master_film| %>
<tr>
<td><%= master_film.serial %></td>
<td><%= master_film.formula %></td>
<td><%= master_film.mix %></td>
<td><%= master_film.mach %></td>
<td><%= master_film.ito_top %></td>
<td><%= master_film.thinky %></td>
<td><%= master_film.chemist %></td>
<td><%= master_film.operator %></td>
<td><%= master_film.inspector %></td>
<td><%= master_film.eff_w %></td>
<td><%= master_film.eff_l %></td>
<td><%= master_film.yield %></td>
<td><%= master_film.defects %></td>
<td><%= master_film.laminated %></td>
<td><%= master_film.note %></td>
<tr>
</tbody>
</table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.