0

I am trying to add an "any" section to my game filters. In a wide view it works fine I just reload the page passing the link the other needed params like this:

  <%= link_to games_path difficulty: params[:difficulty], category: params[:category] do %>
    <div class="game-type-select-content-item">
      <h3>Any</h3>
    </div>
  <% end %>

However, in my mobile view, I'm using a form_with helper:

    <%= form_with url: "/games", method: :get, local: true, data: {target: "game-filters.form"} do |f| %>
      <%= f.label :language, "Language:", :class => "font-bold" %>
      <%= f.select(:language, options_for_select(Language.all.pluck(:name, :language_code), selected: params[:language]), {}, {data: {action: "change->game-filters#submit"}}) %>
      <%= f.label :difficulty, "Difficulty:", :class => "font-bold" %>
      <%= f.select(:difficulty, options_for_select(Game.difficulties.keys, selected: params[:difficulty]), {}, {data: {action: "change->game-filters#submit"}}) %>
      <%= f.label :category, "Category:", :class => "font-bold" %>
      <%= f.select(:category, options_for_select(Game.categories.keys, selected: params[:category]), {}, {data: {action: "change->game-filters#submit"}}) %>
    <% end %>

Is there a way to give each option an "any", that doesn't reset the other functions??

1 Answers1

0

You can add a new option for select by putting this code

If you don't want to use a key for your option, you can use this.

<%= f.select(:difficulty, options_for_select(Game.difficulties.keys, selected: params[:difficulty]), {:include_blank => 'Any'}, {data: {action: "change->game-filters#submit"}}) %>

or if you need a key value, you can add a new array to your options list.

<%= f.select(:difficulty, options_for_select([["Any", "Any"]] + Game.difficulties.keys, selected: params[:difficulty]), {}, {data: {action: "change->game-filters#submit"}}) %>

Maybe this question will be helped too.

  • This does create a selectable 'Any', however it doesn't have the intended function of displaying everything. The params went from this http://localhost:3000/games?language=ja&difficulty=beginner&category=typing to this http://localhost:3000/games?language=%2Fgames&difficulty=beginner&category=typing and nothing is dispalyed. – Ry Ellingson Dec 24 '20 at 18:07