0

在我看来,我有一个带有 text_field_tag 的表单,它接受一个 int,并且只返回 timesheet.user_id 与该 int 匹配的时间表

<%= text_field_tag("user_id", params[:user_id]) %>

然后我有一堆用户,这些用户有一个 nameuser.name和一个 id user.id

我在控制器中有一个名为 pending_approvals 的方法,当给定用户 ID 时,它会将我的列表缩小到特定的用户对象。

if params[:user_id].present?
  @time_sheets = @time_sheets.joins(:user).where("users.id IN (?)", params[:user_id])
end

所以我遇到的问题是,我不确定如何用用户名列表填充我的选择框。然后在提交时,我需要它为该方法提供用户 ID。这样做的最佳方法是什么?

4

1 回答 1

0

如果我理解正确,您是否想要一个集合下拉列表来详细说明所有用户和他们的 ID。你可以做:

<%= collection_select :user, :user_id, @users, :name, :id, :prompt => true %>

其中 :text_method 是在 @users 成员上调用的方法,它将返回您希望出现在下拉列表中的文本。

ApiDocs将分解,这是它的设置方式:

object = User
method = user_id 
collection @users 
value_method = id 
text_method = name 
option = prompt => true 
于 2013-08-07T03:40:08.693 回答