Ruby on Rails - Make view accessible to certain users -


in application have models file & fileaccess. user a can upload files , give access many other users (by other users asking access).

my models:

class file < activerecord::base   belongs_to :user   has_many :file_accesses  class fileaccess < activerecord::base   belongs_to :user   belongs_to :file 

my filescontroller:

class filescontroller < applicationcontroller   def accepted_users      fileaccess.create(user_id: params[:user_id], file_id: params[:file_id], accept: true)   end 

my routes.rb:

get 'i/:user_id/:file_id', to: 'files#accepted_users', as: :file_access_accepted 

my view:

= link_to "give access", file_access_accepted_path(@file, other_user.id) 

other users can ask access & user a can select users want give file access clicking on give access button.

in filescontroller have access_file action & view:

class filescontroller < applicationcontroller   def access_file     redirect_to @file, alert: "you don't have access page!" if @file.user != current_user   end 

currently view/page viewable file owner , if user not file owner, they'll send alert.

how can achieve page/view accessible file owner and other users have been accepted accept: true file owner.

try one

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 page!"   end end 

or

def access_file   if @file.user != current_user && !fileaccess.exists?(file: @file, user: current_user)     redirect_to @file, alert: "you don't have access page!"   end end 

the latter should 1 sql query


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -