Ruby on rails Rake任务访问模型

Ruby on rails Rake任务访问模型,ruby-on-rails,ruby-on-rails-3.1,rake,rails-models,Ruby On Rails,Ruby On Rails 3.1,Rake,Rails Models,我正试图从这样一个rake任务访问一个名为Book的模型 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',

我正试图从这样一个rake任务访问一个名为
Book
的模型

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
但我得到以下警告

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

Tasks: TOP => create_epubs
(See full trace by running task with --trace)
我正在尝试像下面这样加载环境,但对于Rails3.1可能有点不合适

*当我将Book.all.to_yaml放入时,edit Book.all会返回一些内容,因此to_sym错误可能是av.render中的其他错误

我已经知道问题出在哪里了。从我的观点来看,我指的是实例变量

有人能告诉我如何通过设置实例变量来保持使用该变量吗

这是我将实例变量更改为:params变量时的工作版本

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

您可能应该使用实例变量

@book = Book.first 
在你的渲染中

:locals => { :book => @book } 
还有,我想你想要

:layout => nil 

使用--trace选项查看错误发生的位置。它也可能在任务之外。它肯定是第8行的to_sym错误。我在代码中没有看到任何to_sym??我遗漏了什么吗?这将是
:books=>books
这几乎肯定不是
:books=>books
,而是渲染调用中的一些内容。估计您需要设置更多的基础设施才能成功渲染。
--trace
将有助于诊断。