编辑3: 我有一个用户和文档的嵌套设置,用于对用户进行身份验证 当我试图访问我通过这个方法创建的链接时,会出现一个错误,它指向带有ActiveRecord :: RecordNotFound错误的控制器get_document方法
root :to => "users#index"
resources :users do
resources :documents
end
然后我尝试列出所有文件:
@documents.each do |doc|
link_to(doc.title, [@user, doc])
但是我在控制器中尝试访问私有get_user方法时遇到错误,这是执行此操作的核心方法
该文档属于用户,但只提供user_id我是否使用它?
private #get_user should not be called outside contoller
def get_user
@user = User.find(params[:user_id])
end
错误:不应该为out side contoler调用私有方法
编辑: 似乎是被调用的get_document方法......
def get_document
@document = @user.documents.find(params[:id])
end
但文档引用正在传递给路由不是吗?
编辑2: 文件的控制器代码:
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