0
我試圖從一個rake任務訪問一個名爲Book
模型,像這樣Rake任務存取款
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)
我試圖加載環境如下面的accessing rails models from rake task,但也許它是稍微偏離軌道3.1
*編輯Book.all返回一些東西,當我把Book.all.to_yaml所以to_sym錯誤是probabl y 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
使用--trace選項,看看那裏的錯誤正在發生。它也可能在任務之外。 –
它絕對是第8行上的to_sym錯誤 –
我在代碼中沒有看到任何to_sym?我錯過了什麼嗎? –