2017-09-10 1 views
0

私のアプリケーションで私はモデルFile & FileAccessを持っています。 ユーザーAは、ファイルをアップロードして他の多くのユーザーにアクセスできます(他のユーザーがアクセスを求めて)Ruby on Rails - 特定のユーザーが閲覧できるようにする

マイモデル:

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 

マイビュー:アクセス& ユーザーAを求めることができます

= link_to "Give Access", file_access_accepted_path(@file, other_user.id) 

他のユーザーがアクセスにボタンを付けをクリックしてにファイルアクセスを提供したいというユーザーを選択することができます。私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を受け入れてきた他のすべてのユーザがアクセス可能です。

答えて

0

は、後者が

+0

おかげでたくさんの@Ursusちょうど1つのSQLクエリである必要があり、この1

def access_file if @file.user != current_user && [email protected]_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 

を試してみてください。あなたはあなたの時間を感謝します:) –

+0

それはうまくいくのでしょうか?心配しないで、ちょうど2分: – Ursus

+0

うまく働いた。私はちょうど私の使用のためにそれに小さなツインを追加しました:) –

関連する問題