Я попытался отладить эту ошибку, но не могу понять, что не так с моим кодом.Метод Error Rails
Я использую DEViSE камень для пользователей и current_user
является методом Разрабатывает
Я пытаюсь добавить перед действием на мой контроллер документов для редактирования, обновления и уничтожения документа, когда не пользователь, который загрузил Это.
Это ошибка: undefined method 'documents' for nil:NilClass
Ошибка В частный метод authorized_user
Это мой код контроллера:
class DocumentsController < ApplicationController
before_action :find_document, only: [:show, :edit, :update, :destroy, :upvote, :dislike]
before_action :authorized_user, only: [:edit, :update, :destroy]
before_filter :authenticate_user!, except: [:index, :show]
def index
if params[:category].blank?
@documents = Document.all.order(:cached_votes_up => :desc)
if params[:search]
@documents = Document.search(params[:search]).order(:cached_votes_up => :desc)
elsif
@documents = Document.all.order(:cached_votes_up => :desc)
end
else
@category_id = Category.find_by(name: params[:category]).id
@documents = Document.where(category_id: @category_id).order(:cached_votes_up => :desc)
end
end
def show
end
def new
@document = current_user.documents.build
end
def create
@document = current_user.documents.build(documents_params)
if @document.save
redirect_to @document
else
render 'new'
end
end
def edit
end
def update
if @document.update(documents_params)
redirect_to @document
else
render 'edit'
end
end
def destroy
@document.destroy
redirect_to root_path
end
private
def documents_params
params.require(:document).permit(:title, :category_id, :search, :pdf)
end
def find_document
@document = Document.find(params[:id])
end
def authorized_user
@document = current_user.documents.find_by(id: params[:id])
redirect_to documents_path, notice: "Not authorized to edit this Document" if @document.nil?
end
end
Я не знаю, почему я получаю Неопределенный метод для документов
Пожалуйста, покажите нам 'current_user' – Ven
« current_user »- это вспомогательный метод, предоставляемый приложением, которое является камнем, который я использую для пользователей, поэтому он создается не мной –