# app/controllers/research_documents_controller.rb
class ResearchDocumentsController < ApplicationController
  before_action :set_ancestors

  def index
    @research_documents = @respondent_category.research_documents.order(created_at: :desc)
  end

  def create
    @research_document = @respondent_category.research_documents.new(document: params[:research_document][:document])
    if @research_document.save
      redirect_to project_respondent_category_research_documents_path(@project, @respondent_category), notice: "Document uploaded."
    else
      redirect_to project_respondent_category_research_documents_path(@project, @respondent_category), alert: @research_document.errors.full_messages.to_sentence
    end
  end

  def destroy
    @respondent_category.research_documents.find(params[:id]).destroy
    redirect_to project_respondent_category_research_documents_path(@project, @respondent_category), notice: "Document deleted."
  end

  private

  def set_ancestors
    @project = Project.find(params[:project_id])
    @respondent_category = RespondentCategory.find(params[:respondent_category_id])
  end
end
