簡體   English   中英

Rails options_from_collection_for_select顯示除當前用戶外的所有用戶

[英]Rails options_from_collection_for_select display all users except current user

我有一個傳輸模塊,在這里我想在options_from_collection_select里面顯示所有可能的接收者 我設法顯示了所有用戶,但我希望它不顯示所選內容中的當前用戶名,因為用戶不應該收到自己的轉讓。 這就是我現在所擁有的: 請單擊圖像 我目前正在使用ADMIN帳戶,並且您可以看到它顯示在select標簽上。 我只希望它被隱藏。 這是我的代碼:

_form.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>

控制者

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)

暫無
暫無

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

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