在我的应用程序中,我有模型File & FileAccess。用户A可以上传文件并向许多其他用户提供访问权限(由其他用户请求访问)。
我的模特:
class File < ActiveRecord::Base
belongs_to :user
has_many :file_accesses
class FileAccess < ActiveRecord::Base
belongs_to :user
belongs_to :file我的FilesController__:
class FilesController < ApplicationController
def accepted_users
FileAccess.create(user_id: params[:user_id], file_id: params[:file_id], accept: true)
end我的routes.rb__:
get 'i/:user_id/:file_id', to: 'files#accepted_users', as: :file_access_accepted我的观点:
= link_to "Give Access", file_access_accepted_path(@file, other_user.id)其他用户可以请求访问&用户A可以通过单击“给予访问”按钮来选择要给予文件访问权限的用户。
在我的FilesController中,我有一个access_file操作和视图:
class FilesController < ApplicationController
def access_file
redirect_to @file, alert: "You don't have access to this page!" if @file.user != current_user
end当前,此视图/页仅可查看文件所有者,如果用户不是文件所有者,则将发出警告返回。
如何实现它,使文件所有者和可以访问此页面/视图--所有已被文件所有者接受accept: true的其他用户。
发布于 2017-09-10 23:00:02
试试这个
def access_file
if @file.user != current_user && !@file.file_accesses.map(&:user).include?(current_user)
redirect_to @file, alert: "You don't have access to this page!"
end
end或
def access_file
if @file.user != current_user && !FileAccess.exists?(file: @file, user: current_user)
redirect_to @file, alert: "You don't have access to this page!"
end
end后者应该只是一个SQL查询。
https://stackoverflow.com/questions/46145904
复制相似问题