正如 Deefour 提到的,您将需要手动执行此操作。你在这里解释的问题是授权而不是身份验证,看看像CanCan这样的 gem
用一个例子来解释它。您必须确保用户是给定组织的成员。这可能看起来像这样(假设您有一个代表登录用户的 current_user):
控制器:
class UsersController < ApplicationController
before_filter :find_organization, :ensure_organization_membership, :only => :show
def show
@user = @organization.users.find(params[:id])
end
def find_organization
@organization = Organization.find_by_name(params[:org_name])
end
def ensure_organization_membership
# Make sure the current_user(Logged in user) is a member of the company before showing the user profile
@organization.include(:users).member_of?(current_user)
end
end
并且在模型中
class Organization
....
def member_of?(user)
users.includes?(user)
end
...
end
希望有帮助。