0

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
Filip
  • 118
  • 1
  • 1
  • 8
  • make get_user method regular method not private and see what's happens. Problem is probably not nested routes related, but without knowing controller code and place where you defined get_user method I am not able to help you more. Add controller code and whatever it is where get_user is defined. Label code with file paths please. – Kocur4d Mar 04 '13 at 14:41
  • link_to uses [@user, doc] just to build a path to resource it is not calling any of your methods so that's why I think that problem is somewhere else. To check if link_to causes a problem, comment it out and see if problem is persisted if yes that's mean that you need to look somewhere else. – Kocur4d Mar 04 '13 at 14:52
  • I get the problem when i click the link, but you think it the loaded page instead? Your hunch about the problem not going away when I mad get_document public was right – Filip Mar 04 '13 at 14:56
  • so when the page with link on it is loading fine that mean link_to is ok. You can verify it by showing page source and checking the link href it should be something like "/users/.../documents/..". Problem is probably in a controller action link hits or in view that this action is about to render. – Kocur4d Mar 04 '13 at 15:02
  • No I found the problem I found the problem and sorry for wasting your time. The test deleted the user and I had forgotten to set the :dependent :destroy in documents model. thanks for your help probably save me 2 hours of work – Filip Mar 04 '13 at 15:07
  • not a problem, have a good time – Kocur4d Mar 04 '13 at 15:10

0 Answers0