Take the 2-minute tour ×

I'm trying to access a model called Book from a rake task like so

task :create_epubs => :environment do
  include Rails.application.routes.url_helpers # brings ActionDispatch::Routing::UrlFor
  include ActionView::Helpers::TagHelper

  av = ActionView::Base.new(Rails.root.join('app', 'views'))

  books = Book.all
  av.render("books/", :books => books)

but i get the following warning

rake aborted!
undefined method `to_sym' for nil:NilClass

Tasks: TOP => create_epubs
(See full trace by running task with --trace)

I'm trying to load environment like the following accessing rails models from rake task but maybe it's slightly off for rails 3.1

*edit Book.all returns something when I do puts Book.all.to_yaml so the to_sym error is probably something else in av.render

I've figured out what the problem is. I was referring to instance variable from my view.

Can anyone tell me how to keep using instance variables by setting that variable?

This is the working version when I change the instance variables to the :params variables

task :create_epubs => [:environment] do
  av = ActionView::Base.new(Rails.root.join('app', 'views'), :assigns => self)
  av.view_paths = ActionController::Base.view_paths
  av.extend ApplicationHelper #or any other helpers your template may need

  book = Book.first

  puts av.render(:template => "books/epub-show", :locals => {:book => book}, :layout => false) # this isn't passing @book to the view correctly, i get undefined method for nil:nilClass
share|improve this question
use the --trace option to see where the error is occuring. It could be outside the task as well. –  Wahaj Ali Jan 23 '12 at 16:19
it definitely the to_sym error on line 8 –  Joseph Le Brech Jan 23 '12 at 16:21
I don't see any to_sym in the code?? Am i missing something? –  Wahaj Ali Jan 23 '12 at 16:23
it'll be this :books => books –  Joseph Le Brech Jan 23 '12 at 16:24
It's almost certainly not the :books => books but something inside the render call. At a guess you need to set up more infrastructure for the render to be successful. The --trace would help to diagnose a lot. –  Shadwell Jan 23 '12 at 17:24
show 4 more comments

1 Answer

you should probably use instance variables.

@book = Book.first 

and in your render

:locals => { :book => @book } 

also, i think you want

:layout => nil 
share|improve this answer
add comment

Your Answer


