使用轨道"选择"方法的路径下拉菜单



从这个文档中我看到我可以像这样使用选择方法:

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })

,它产生:

<select name="post[person_id]">
  <option value=""></option>
  <option value="1" selected="selected">David</option>
  <option value="2">Sam</option>
  <option value="3">Tobias</option>
</select>
如果我有一个这样的数组,我的select方法会是什么样子呢?
[["Add Post", new_post_path],["Add Document", new_document_path],["Add Coupon", new_coupon_path]]

我想要这样的html:

<select name="post[person_id]">
  <option value="new_post_path" selected="selected">Add Post</option>
  <option value="new_document_path">Add Document</option>
  <option value="new_coupon_path">Add Coupon</option>
</select>

在你的控制器中:

@paths = [ ["Add Post", new_post_url], ["Add Document", new_document_url], ... ]

在你的视图

select("post", "person_id", @paths, { :include_blank => true })

这将把实际的URL放在value字段中。如果你想要上面提到的字符串,但是用引号括起来的路径,例如["Add Post", "new_post_path"]

最新更新