Why aren't the :locals hash variables being passed in to a partial, when called from inside my rake

Posted by marshally on Stack Overflow See other posts from Stack Overflow or by marshally
Published on 2010-05-02T03:01:25Z Indexed on 2010/05/02 3:07 UTC
Read the original article Hit count: 507

Filed under:
|
|

I need to render a bunch of painfully long running partials using a rake task.

When I try to pull the partial from a rake task, I get the dreaded "Called id for nil, which would mistakenly be 4" error, which usually means that my locals hash has not been properly set into the partial.

Here's the rake task (some variable names have been changed to protect the innocent):

namespace :precache do
  desc "Precache stuff"
  task :precache => :environment do
    av = ActionView::Base.new(Rails::Configuration.new.view_path, {})
    av.class_eval do
      include ApplicationHelper
    end

    @user = User.find(21)
    @rank = Rank.find(2)

    data = av.render(:partial => "reports/listing", :locals => {:user => @user, :rank => @rank})
  end
end

And this is the error that I am getting:

** Execute precache:precache
rake aborted!
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
On line #1 of app/views/reports/listing.html.erb

    1: <%- @rid = @rank.id %>
    2: <%- @cid = @user.id %>
    3: <%- cache(:action => 'reports', :key => [arg1, arg2, arg3] ) do %>
    4: <%-

    app/views/reports/_downline_js.html.erb:1
    lib/tasks/precache_fragments.rake:12
    rake (0.8.7) lib/rake.rb:636:in `call'
    rake (0.8.7) lib/rake.rb:636:in `execute'
    rake (0.8.7) lib/rake.rb:631:in `each'
    rake (0.8.7) lib/rake.rb:631:in `execute'
    rake (0.8.7) lib/rake.rb:597:in `invoke_with_call_chain'
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    rake (0.8.7) lib/rake.rb:590:in `invoke_with_call_chain'
    rake (0.8.7) lib/rake.rb:583:in `invoke'
    rake (0.8.7) lib/rake.rb:2051:in `invoke_task'
    rake (0.8.7) lib/rake.rb:2029:in `top_level'
    rake (0.8.7) lib/rake.rb:2029:in `each'
    rake (0.8.7) lib/rake.rb:2029:in `top_level'
    rake (0.8.7) lib/rake.rb:2068:in `standard_exception_handling'
    rake (0.8.7) lib/rake.rb:2023:in `top_level'
    rake (0.8.7) lib/rake.rb:2001:in `run'
    rake (0.8.7) lib/rake.rb:2068:in `standard_exception_handling'
    rake (0.8.7) lib/rake.rb:1998:in `run'
    rake (0.8.7) bin/rake:31
    /usr/bin/rake:19:in `load'
    /usr/bin/rake:19

details: I'm using Rails 2.3.5 and Ruby 1.8.7. Developing on Mac OSX. Eventually I will be deploying to Heroku.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rake