繁体   English   中英

rails select2字段未返回表单

[英]rails select2 field not returned into form

这可能是未答复的Select2-rails将数据传递到表单字段的变体

我在表单中有一个字段barrister_id

<%= form_for(@mail) do |f| %>
  ...
  <div class="field">
    <%= f.label :barrister_id %><br />
    <!-- %= f.collection_select :barrister_id, Barrister.all, :id, :code_and_name % -->
    <% @barristers = Barrister.all %>
    <%= select_tag :barrister_id,   options_from_collection_for_select(@barristers,
      :id,:code), id: "barristerselect"%>
  </div>

application.html.erb将此链接到javascript Select2 gem,因此:

<script>
  $(document).ready(function() { $("#barristerselect").select2(); });
</script>

并为我提供了精美的Select2值下拉菜单。

但是字段值barrister_id仅在params.barrister_id中返回,而在params.mail.barrister_id中未按预期返回,因此输入的值验证为空白将失败。

如注释所示,我本来希望将f.collection_select用于选择框,但无法使Select2与之配合使用。

大家有什么想法吗?

在这种情况下,首先应使用f.select。 使用select_tag,您需要执行此操作

<%= select_tag "mail[barrister_id]",   options_from_collection_for_select(@barristers,
  :id,:code), id: "barristerselect"%>

如果您使用的是f.select,请使用它。 注意空的花括号。 这是因为html_options是f.select辅助函数的第四个参数。 此处查阅文档。

<%= f.select :barrister_id, options_from_collection_for_select(@barristers,
  :id,:code), {}, id: "barristerselect" %>

这是因为您更改了选择控件的ID。 尝试将id更改为mail_barrister_id(由select_tag帮助程序生成的默认ID)。 还应该在select2的Java脚本中更改id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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