0

我正在尝试在我的游戏过滤器中添加“任何”部分。在广泛的视图中它工作正常我只是重新加载页面传递链接其他需要的参数,如下所示:

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

但是,在我的移动视图中,我使用的是 form_with 助手:

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

有没有办法给每个选项一个“任何”,而不是重置其他功能?

4

1 回答 1

0

您可以通过输入此代码为选择添加一个新选项

如果您不想为您的选项使用密钥,您可以使用它。

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

或者 如果您需要一个键值,您可以将一个新数组添加到您的选项列表中。

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

也许这个问题也会有所帮助。

于 2020-12-24T04:10:38.327 回答