【问题标题】:Why dropdown list items do not show at my simple_form_for collection code when I render the page?为什么当我呈现页面时,我的 simple_form_for 集合代码中不显示下拉列表项?
【发布时间】:2023-03-29 09:13:01
【问题描述】:

我正在尝试在我的页面中插入一个 simple_form 下拉列表,但在显示列表项时遇到了问题(两个电子邮件地址 - conatct@test.com 和 support@test.com)。当我单击下拉列表箭头时,它们根本不显示,因为它们不存在。

.reveal.doubt id="doubt-material-#{material.id}" data-reveal=true
  button.close-button data-close=true aria-label="Close reveal" type="button"
    span aria-hidden="true" ×
  h5 #{t 'students.materials.index.questions.button'}
  p #{t 'students.materials.index.questions.form_explanation'}
  = simple_form_for [:student, trail, component, material, material_student, doubt], html: { id: "doubt" }, remote: true do |f|
    = f.input :recipient, collection => %w(contact@test.com, support@test.com), label: "#{t 'students.materials.index.questions.form_send_email'}"
    = f.input :question, as: :text, label: true, label: "#{t 'students.materials.index.questions.form_message'}" , input_html: { rows: "2" }
    = f.submit "#{t 'students.materials.index.questions.form_send_button'}", class: 'button primary-button-active'

当我检查代码时,它似乎在工作,但我在任何地方都看不到电子邮件选项。

我添加的代码行是:

= f.input :recipient, collection: %w(contact@test.com, support@test.com), label: "#{t 'students.materials.index.questions.form_send_email'}"

这里有什么问题?

【问题讨论】:

  • 去掉标签会怎样?看起来你可能会得到你想要的东西。 github.com/plataformatec/simple_form#collections 试试下面看看是否有帮助 = f.input :recipient, collection => %w(contact@test.com, support@test.com)
  • 也不起作用。已经尝试过并查看了文档。我注意到 Select2 已“安装”在应用程序中。你认为这可能是个问题吗?

标签: ruby-on-rails jquery-select2 simple-form


【解决方案1】:

我相信您的收藏需要为每个选项设置namevalue。你可以试试:[["contact@test.com", "contact@test.com"], ["support@test.com", "support@test.com"]]。或者使用 Rails options_for_select 辅助方法:

= f.input :recipient, collection => options_for_select(%w[contact@test.com contact@test.com])

【讨论】:

  • 谢谢,但没有一个对我有用。我已经尝试了第一个建议,但没有成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2018-07-07
相关资源
最近更新 更多