ruby-on-rails Rails options_from_collection_for_select显示除当前用户之外的所有用户

uinbv5nw  于 2023-03-20  发布在  Ruby
关注(0)|答案(1)|浏览(88)

我有一个传输模块,我想显示options_from_collection_select中所有可能的接收者。我设法显示了所有用户,但我不想显示当前选择的用户名,因为用户不应该接收他自己的传输。这是我现在所拥有的:please click for the image。我目前使用的是ADMIN帐户,如您所见,它出现在select标记中。我只希望它隐藏起来。以下是我的代码:

_表单.html.erb:

<div class="form-group">
 <div class="col-md-4">
    <%=  f.label :administrator, "To be Receive by", class: "control-label" %>
    <%= f.select :administrator_id,options_from_collection_for_select(@administrators.order("name"), :id, :name, :selected => f.object.administrator_id),{}, class:"select2 form-control", include_blank: 'Select Receiver',required: true %>
 </div>
</div>
ioekq8ef

ioekq8ef1#

主计长

class UserController < ApplicationController
  def index
    @users = User.where.not(id: current_user.id)
  end
end

您可以在视图中访问示例变量@users

或者

您可以使用下面给出的范围变量my explanation来了解如何使用

型号

class User < ApplicationRecord
  scope :all_except, ->(user) { where.not(id: user) }
end

controller中访问范围变量

控制器

@users = User.all_except(current_user)

相关问题