Home>

ransackI created a search form in application.html.erb using.
I want to display the search results on another page and sort the displayed items, but it doesn't work.
If i use the select box, it will work as I want.link_toI want to use. I think that the cause is that there are no parameters at the time of search, but even if I look it up, I can only find the one that "sorts by table column" and I do not know how to realize it. I think that it is different from the one I checked because the sorting condition is a calculation formula or a column in another table.
I think the explanation is difficult to understand, but could anyone please teach me?


<% = search_form_for @search, url: search_recipes_path do | f |%>
    
      Free Word Search
    <% = f.text_field: title_or_body_cont_any, placeholder: "\ uf002 Cooking/Ingredients", class:'fa'%>
    
    
    <% = f.submit'Search', class: "button is-danger is-rounded is-small mt-1"%>
    
    
      Display order
    <% = search_form_for @search, url: search_recipes_path, html: {method:: post} do | f |%>
      <% = f.select (: sorts, {'Please select':'id desc','Popularity':'favorites_count desc','New arrival':' created_at desc','Most comments': 'coment_count desc','highest rating':'star_count desc'}, {selected: params [: q] [: sorts]}, {onchange:'this.form.submit ()'})%>
    <% end%>
    
<% end%>


abovef.selectThe part belowlink_toI want to move with


<% = search_form_for @search, url: search_recipes_path do | f |%>
    
      Free Word Search
    <% = f.text_field: title_or_body_cont_any, placeholder: "\ uf002 Cooking/Ingredients", class:'fa'%>
    
    
    <% = f.submit'Search', class: "button is-danger is-rounded is-small mt-1"%>
    
    
    <% = link_to'in order of popularity', {: q =>{: sorts =>"favorites_count desc"}}%>
    <% = link_to'new arrival order', {: q =>{: sorts =>"created_at desc"}}%>
    <% = link_to'in order of number of comments', {: q =>{: sorts =>"coment_count desc"}}%>
    <% = link_to'Highest rating', {: q =>{: sorts =>"star_count desc"}}%>
    
<% end%>
  • Answer # 1

    As belowsort_linkI was able to deal with this by fixing the direction of sorting.

    <% = sort_link (search,:'favorites_count desc','popular order', hide_indicator: true)%>
    <% = sort_link (search,:'created_at desc','new arrival order', hide_indicator: true)%>
    <% = sort_link (search,:'coment_count desc','in order of number of comments', hide_indicator: true)%>
    <% = sort_link (search,:'star_count desc','highest rating', hide_indicator: true)%>


    Thank you for watching.