在 Rails 简单表单中编辑时在下拉列表中显示所选选项

Show selected option in dropdown on edit in Rails Simple Form

我正在尝试用 rails 4 和简单的形式制作一个应用程序。

当我想编辑一个已保存的项目时,我被带回我的表单,该表单询问显示原始问题,但没有在创建时选择输入。

有没有办法设置表单,以便在单击“编辑”时显示之前的选择?看来我还没有以显示编辑表单的方式选择输入。

例如,我的一个问题是:

<%= par.select :location,
   options_for_select(["London", "Paris", "Berlin"]),
   label: 'Where will participants take part in this project?',
   prompt: "Choose one",
   class: 'response-project' %>

当我在创建时选中伦敦时,它会保存该选择。当我然后按编辑时,表单显示 'choose one'。看来我还没有拯救伦敦(我已经拯救了 - 数据库显示了那个选择)。

您可以尝试以下方法将当前位置设置为选定选项而不是提示选项:

options_for_select(["London", "Paris", "Berlin"], selected: :location)