How to show only those elements that belong to a specific user (using restful_authentication)?

I have a web application with users and their documents. Each user can have many documents:

user.rb:

has_many :documents

document.rb:

belongs_to :user

document_controller.rb:

def index
    @documents = Document.find(:all)
end

I am using the restful_authentication plugin. Here is my question: how to make the controller display only documents belonging to each user? Now it displays all documents for all users.

I am using the latest version of Rails.

+3
source share
4 answers

Have a look here in the rails APIs under "Merging Partitions".

, Restful authentication , , , . - , URL- .

Restful ACL

+2

User Document. , , :

def index
  @documents = @current_user.documents
end

. .

+7
def index
  @documents = Document.find(:all, :conditions => {:user_id => session[:user_id]})
end
+3

:

def index
    @documents = Document.where(:user_id => current_user.id)
end
+3

Source: https://habr.com/ru/post/1698455/


All Articles