Rake 任务访问模型

发布于 2024-12-28 07:20:21 字数 1422 浏览 2 评论 0 原文

我正在尝试从 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)

我正在尝试加载如下所示的环境 从 rake 任务访问 Rails 模型,但对于 Rails 3.1 来说可能略有偏差

*编辑 Book.all 当我放置时会返回一些内容Book.all.to_yaml 所以 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2025-01-04 07:20:21

您可能应该使用实例变量。

@book = Book.first 

在你的渲染中

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

,我认为你想要

:layout => nil 

you should probably use instance variables.

@book = Book.first 

and in your render

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

also, i think you want

:layout => nil 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文