簡體   English   中英

Rails 3 - select_tag helper - array

[英]Rails 3 - select_tag helper - array

我有一個問題,我有點尷尬地問,但似乎無法弄明白。

我正在編寫一個表單,允許用戶過濾信息,以便只查看他們想要的內容。 在表單中,我使用select_tag幫助程序作為下拉菜單。 選擇菜單由一組用戶填充。 我有以下代碼:

<% @users.each do |user| %>
  <%= select_tag "users", options_for_select([user.name]) %> 
<% end %>

這個問題是它為@users數組中的每個user.name生成一個選擇菜單。 我假設問題是我在@users上使用.each。 但是,今天早上我一直在努力解決這個問題,所以我想我只會問......

在使用select_tag幫助程序時,從數組中獲取項目以填充到單個標記的正確方法是什么?

謝謝

您不必遍歷用戶。 你可以這樣做:

<%= select_tag "users", options_from_collection_for_select(@users, "id", "name") %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM