自定义options_for_select输出?

时间:2012-04-11 20:15:22

标签: ruby-on-rails ruby-on-rails-3

我的select_tag options_for_select看起来像这样:

<%= select_tag "respondent", options_for_select(1..5, 2) %>

输出:

<select id="respondent" name="respondent">
  <option value="1">1</option>
  <option value="2" selected="selected">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>

但我希望它输出的是:

<select id="respondent" name="respondent">
  <option value="/arbitrary/page/1">#1</option>
  <option value="/arbitrary/page/2" selected="selected">#2</option>
  <option value="/arbitrary/page/3">#3</option>
  <option value="/arbitrary/page/4">#4</option>
  <option value="/arbitrary/page/5">#5</option>
</select>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您有arbitrary_page_path这样的命名路线:

<% collection = (1..5).map { |i| ["##{i}", arbitrary_page_path(i)] } %>
<%= select_tag "respondent", options_for_select(collection, arbitrary_page_path(2)) %>