Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:33:06+00:00 2026-05-30T01:33:06+00:00

I would love to call a controller action from within a model. Yes, MVC.

  • 0

I would love to call a controller action from within a model. Yes, MVC. Thanks.

So, why would i like to call a controller action and fetch the response?

Because my controller knows how to render the file I would like to cache. Why should I duplicate code to collect all data needed by my view?

Setup:

  • InvoicesController responds_to :html, :pdf
  • Invoice uses state_machine (:new -> :open -> paid)

What I “need” within the state transition from :new to :open

  • generate /invoices/:id.pdf
  • cache the PDF as Invoice#file for later use in delayed_job or simliar

What interface i would love to use elsewhere?

  • @invoice.build_pdf

Any suggestions?

Update:
I would like to cache the PDF as model attachement for later use (delayed_job mailing, etc)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:33:07+00:00Added an answer on May 30, 2026 at 1:33 am

    It may be better to take the code you’re using in your controller to render the PDF data, and create a module or class in your lib directory let’s say lib/pdf.rb then use that code in both your controller and your model.

    Update: You can cache the PDF itself on the filesystem as a file with a timestamp as it’s name or something like that that is uniquely identifiable. Then add a file or invoice attribute to the model to store the location in the database for later use.

    Update: You can use the block form of respond_to like so:

    respond_to do |format|
      format.html
      format.pdf  { InvoiceBuilder.create(@invoice) }
    end
    

    Update: It seems that ActionDispatch::Integration::Session is a class for creating and interface for integration testing. But I think you could exploit it for your purposes.
    You should be able to use it like this:

    # app/models/invoice.rb
    class Invoice < ActiveRecord::Base
      def get_body
        app = ActionDispatch::Integration::Session.new(Rails.application)
        app.get("invoices/#{self.id}.pdf")
        app.response.body # this returns the rendered content that the browser will see.
      end
    end
    

    Another way would be to grab the content by using curl i.e. curl http://localhost:3000/invoices/1.pdf and store the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would love to get some tips from other people that have had this
I have a .NET MVC controller action that returns a JsonResult to a YUI
In Spring 3 MVC, I have a controller that I call SettingsController, and it
I would like to call a c# Method everytime a jquery UI slider event
I would love to be able to have a common set of Classes in
I would love to be able to do >>> A = numpy.array(((1,2),(3,4))) >>> idx
Ok here's a little problem I would love to get some help on. I
Working on a movie website and would love to find an API that I
I just started getting into BizTalk at work and would love to keep using
We're about to build a Blackberry application but would love some input on whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.