SupremeA
SupremeA

Reputation: 1621

Calling an instance of an object into another controller rails

I have a report object that renders fine, however I have a reportapproval object that I want to pass to the report view so I can run a calculation within the report view. Each user has_one report and each report has multiple reportapprovals. How can I call a specific reportapproval and send that instance to the report controller or view. Here is my report controller.

class Users::ReportsController < ApplicationController
  def show
      @report = Report.find(params[:id])
      authorize @report
  end
end

I tried to pass

@reportapproval = Reportapprovals.where(user_id: @report.user_id).first

in the show method but the ReportController wont recognize the Reportapproval.

How can I pass the instance to the view?

Upvotes: 2

Views: 776

Answers (2)

Prashant4224
Prashant4224

Reputation: 1601

You can fetch reportapprovals records in show method itself, The @report and @reportapproval you can access on views reports/show.html.erb

class Users::ReportsController < ApplicationController
  def show
    @report = Report.find(params[:id])
    @reportapproval = @report.reportapprovals.first
    authorize @report
  end
end

Upvotes: 1

Steven Yue
Steven Yue

Reputation: 1010

I think you can just do

@reportapproval = @report.reportapprovals.first

Upvotes: 2

Related Questions