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 6799143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:48:48+00:00 2026-05-26T18:48:48+00:00

I am inside a Rails controller and I am trying to access my instance

  • 0

I am inside a Rails controller and I am trying to access my instance variable in a block:
This gives an error saying that “no method field1 for Nil”:

Prawn::Document.generate("hello.pdf") do
  @model.field1
end

However, if I do this, then it works:

my_model = @model
Prawn::Document.generate("hello.pdf") do
  my_model.field1
end

Could this have something to do with ActiveRecord accessors or instance variables in a block?

  • 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-26T18:48:49+00:00Added an answer on May 26, 2026 at 6:48 pm

    That’s happening because code inside block is executed in context of Prawn::Document object. Let’s go inside this code:

    module Prawn
      class Document
        def self.generate(filename,options={},&block)
          pdf = new(options,&block)
          pdf.render_file(filename)
        end
    
        def initialize(options={},&block)
          if block
            block.arity < 1 ? instance_eval(&block) : block[self]
          end
        end
      end
    end
    

    As you can see, block is executed with Document object as self. It try to find @model as instance variable of self, can’t do this and return nil. If you use local variable model, you get help of closures and your code is working properly

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

Sidebar

Related Questions

I have a Rails controller where I accidentally defined the 'edit' method inside the
I have a REST API that I am trying to access using Rails 3.0.1
I've set up a Rails 3 proxy method inside a controller to use Nginx'
I need to be able to quickly convert an image (inside a rails controller)
I'm trying to pass a byte array from inside my rails app into another
I'm trying to use Chronic inside my non-rails project. When I try to get
Inside VS2005, our whole programming staff gets this error message sporadically and it is
In my rails application I am trying to access pagination via an ajax call.
I'm trying to get my backbone associations working inside a rails app , and
In Ruby on Rails, how is an ActionView object able to access the instance

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.