我有一个可以正常工作的 simple_form ui,我f.input :group_ids
用于将 Poll 分配给 Group(Poll has_and_belongs_to_many
:groups,但我们将其限制为 UI 中的一个)。
无论如何,我想要一种更优雅的方式来做 UI,但是当验证失败时它也会导致严重的问题——输入恢复为具有值的文本输入:[4],其中 4 是当前组的 id。(除了看起来很糟糕之外,除非手动删除括号,否则提交失败)
<%= simple_form_for @poll do |f| %>
<% if params[:group] %>
<%= f.input :group_ids, :label => "Group", :selected => params[:group], :collection => @groups, :include_blank => false, :input_html => {:multiple => false} %>
<% else %>
<%= f.input :group_ids, :label => "Group", :collection => @groups, :include_blank => false, :input_html => {:multiple => false} %>
<% end %>
...
<% end %>
我希望有更好的方法来做到这一点——尝试使用 f.association,但无法弄清楚如何将其限制为单选下拉菜单。