简体   繁体   中英

Sort Date (Newest to Oldest) ROR

this is 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>

I need to organize the Date column in desc (newest to oldest) dates. I added the following dropdown option but it fails. Error template. Or if I can default the sort to newest Laminated date is first.

 <%= render 'shared/sort_dropdown', current: sort, choices: [['serial','desc'], ['laminated','desc']] %> 

I am writing this in detail, assuming you are very new to rails because of the way you asked the question and the way your code of view is. My apologies, if I am wrong.

In your controller (I guess it should be films_controller.rb or master_films_controller.rb ), the action in which you are calculating this @master_films (it will have same name as the view that has the code you posted in question), add this:

  @master_films = MasterFilm.all.order('laminated DESC')
  # OR
  @master_films = MasterFilm.all.order(laminated: 'desc')
  # letter case does not matter

Your table code in view is not correct. It should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM