0

我有这样的任务:当用户编辑他的对象时 - 列表中的第一个选项将是对象的值。

我正在使用以下代码生成我的选择列表:

    <%= f.select :category, options_for_select([
      "#{@website.category}",
      "Banking",
      "Computers",
      "Coupons",
      "Directory",
      "Fashion",
      "Finance",
      "Gifts",
      "Maps",
      "Media",
      "Mobile",
      "News",
      "Tickets",
      "Tech blog",
      "Tech website",
      "Trains",
      "Travel",
      "Transportation - general",
       "Recipes",
      "Another... "])%>

谁能建议怎么做?

4

1 回答 1

1

请参阅此处有关options_for_select的文档

您可以将选择框的默认值(或选定值)设置为第二个参数。在你的情况下,它会像:

<%= f.select :category, options_for_select([
  "Banking",
  "Computers",
  "Coupons",
  "Directory",
  "Fashion",
  "Finance",
  "Gifts",
  "Maps",
  "Media",
  "Mobile",
  "News",
  "Tickets",
  "Tech blog",
  "Tech website",
  "Trains",
  "Travel",
  "Transportation - general",
   "Recipes",
  "Another... "], @website.category)%>

希望这可以帮助!

于 2012-10-05T13:47:23.733 回答