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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:42:08+00:00 2026-06-11T16:42:08+00:00

Caching is by far the most logic-intensive part of my view code, so I

  • 0

Caching is by far the most logic-intensive part of my view code, so I would like to do fragment caching from inside a decorator, however, I cant do it.

When i do this from my decorator:

def cached_name
  h.cache do
   "a name here"
  end
end

I get this:

You have a nil object when you didn’t expect it! You might have
expected an instance of Array. The error occurred while evaluating
nil.length

I instantiate my decorator from inside a controller

@presenter = SomePresenter::new

I am using HAML for my views

How can I succesfully cache from inside my decorator, so my view can do stuff like this

= @decorator.cached_logic_heavy_stuff

UPDATE: I have created a git repo showing my issue: https://github.com/houen/presenter_caching

UPDATE: This maybe works – see the repo

  include Haml::Helpers
  def another_way_to_try
    self.init_haml_helpers
    buffer = haml_buffer.buffer

    h.with_output_buffer(buffer) do
      h.cache do
        h.concat "i should still not be empty"
      end
    end
  end
  • 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-06-11T16:42:09+00:00Added an answer on June 11, 2026 at 4:42 pm

    Rails’ cache method tries to infer a cache key based on the view that it’s being called from. Since you’re not actually calling it from a view (but from inside an instance of a decorator class), I expect that it’s bombing when trying to build a cache key.

    You might try passing a cache key explicitly, via h.cache "your cache key" do. With a full stack trace, you can figure out where it’s throwing the exception, and then work around that, as well. Without the full stack trace, it’s harder to help you, though.

    Edit: Looking at Rails’ caching code, I think this might be a deeper issue; it’s attempting to get the length of output_buffer, which isn’t going to be available outside of your views’ contexts (that is, within Draper). You might try adding:

    def output_buffer
      h.output_buffer
    end
    

    But without testing it, I’m thinking it might not work exactly as planned without some more work. This is just a rough guess – I’d be surprised if this is actually the issue, but hopefully it gets you on the right path.

    The note in the source there:

    # VIEW TODO: Make #capture usable outside of ERB
    # This dance is needed because Builder can't use capture
    

    indicates that this isn’t a fully-solved problem, so you may need to do a little digging around in the Rails internals to make this one work.

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

Sidebar

Related Questions

I am catching keys for bindings, so far most characters work fine, until I
I'm planning on caching some information using ehcache. Normally, I would follow the pattern:
How do you prevent Firefox and Safari from caching iframe content? I have a
I'm looking for a Python caching library but can't find anything so far. I
I read http://code.google.com/speed/page-speed/docs/caching.html . It says that proxy servers may cache cookies. I need
I want to use data caching on my .net C# application. So far I
I've started testing out Nancy in self-host mode. So far, so good apart from
Trying to get fragment caching to work in Rails 3.0.9. I have set this
I really like the ability to edit javascript in the chrome debugger however, I
I've installed django-redis-cache and redis-py. I've followed the caching docs for Django. As far

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.