0

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)
end

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
end
Community
  • 1
  • 1
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
  • 1
    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
  • I think I need to load more of rails as the routes weren't working, I've managed to get it working inside of the views but they are missing path helpers. – Joseph Le Brech Jan 24 '12 at 09:21
  • Don't you think here `av.render("books/", :books => books).to_string` should be `av.render("books/partial_name", :books => books).to_string` ?? Or correct me if I'm wrong!! – Surya Jan 24 '12 at 10:28
  • I've changed it to the right partial, it loads that partial correctly but it just not passing the variable to it, although i can access the model form the task itself. – Joseph Le Brech Jan 24 '12 at 10:33
  • I've figure that i'm not setting an instance variable when using `:locals = {}` so i have to change `@book` to `book` in the view. – Joseph Le Brech Jan 24 '12 at 11:11

1 Answers1

0

you should probably use instance variables.

@book = Book.first 

and in your render

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

also, i think you want

:layout => nil 
edwardsharp
  • 1,232
  • 10
  • 17