编辑3:当我尝试访问通过此方法创建的链接时,我为用户和文档设置了嵌套设置,该设计用于对用户进行身份验证
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
错误:不应为外部控制器调用私有方法
编辑:似乎是被调用的 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