EDIT 3: I have a nested setup for users and document with devise used for authentication on users when I trie to access a link I create through this method an error accours which points me to the controller get_document method with a ActiveRecord::RecordNotFound error
root :to => "users#index"
resources :users do
resources :documents
end
Then i try to list all the documents with:
@documents.each do |doc|
link_to(doc.title, [@user, doc])
But I get an error trying to access the private get_user method in my controller what is the corect way to do this
The document belongs to a user but that only gives the user_id do I use that?
private #get_user should not be called outside contoller
def get_user
@user = User.find(params[:user_id])
end
error: private method should not be called for out side contoler
EDIT: It seems it is the get_document method that is being called...
def get_document
@document = @user.documents.find(params[:id])
end
But the document reference is being passed to the route Isn't it?
EDIT 2 : conttroller code for document:
class DocumentsController < ApplicationController
before_filter :get_user
before_filter :get_document, :only => [:show, :update, :edit, :destroy]
before_filter :authenticate_user!
load_and_authorize_resource
def new
@document = @user.documents.build
end
def index
if params[:tag]
@documnets = Document.tagged_with(params[:tag])
else
@documents = Document.all
end
end
def create
@document = @user.documents.build(params[:document])
if @document.save
flash[:notice] = "Document has been created."
redirect_to [@user, @document]
else
flash[:notice] = "Failed to create document."
render :action => "new"
end
end
def edit
end
def show
end
def update
if @document.update_attributes(params[:document])
flash[:notice] = "Update Complete"
redirect_to [@user, @document]
else
flash[:notice] = "Update Failed"
render :action => "edit"
end
end
def destroy
if @document.destroy
flash[:notice] = "document deleted."
redirect_to @user
else
flash[:notice] = "Can't delete document"
redirect_to [@user, @document]
end
end
private #get_user should not be called outside contoller
def get_user
@user = User.find(params[:user_id])
end
public
def get_document
@document = @user.documents.find(params[:id])
end
end