Rails 3:如何在rake任务中渲染ERb模板?

时间:2010-11-23 23:19:52

标签: ruby-on-rails-3 rake erb

我使用rake渲染一个非常庞大的站点地图HTML文件。不幸的是,当我迁移到rails 3时代码中断。我当前的代码如下所示:

@controller = ActionController::Base.new
@controller.request = ActionController::TestRequest.new
@controller.instance_eval do
  @url = ActionController::UrlRewriter.new(request, {})
end

# collect data, open output file file

template = ERB.new(IO.read("#{RAILS_ROOT}/app/views/sitemap/index.html.erb"))
f.puts(template.result(binding))

此代码在2.3中工作,但在Rails 3中断,因为url_for不再访问@controller,而是控制器。 (我想这就是原因。)

undefined local variable or method `controller' for #<Object:0x3794c>
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
(erb):5
/Users/me/Documents/Projects/zvg2/lib/tasks/zvg.rake:452

我也尝试过创建一个ActionView来做到这一点:

av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).view_path, {
  # my assigns
}, @controller)
av.class_eval do
  include ApplicationHelper
end
f.puts(av.render(:template => "sitemap/index.html"))

但问题似乎是一样的,虽然ActionView :: Base.new需要我的控制器。

undefined local variable or method `controller' for nil:NilClass
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/whiny_nil.rb:48:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/Users/me/Documents/Projects/zvg2/app/views/sitemap/index.html.erb:5:in `_app_views_sitemap_index_html_erb__757224102_30745030_0'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:135:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:135:in `render'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:54:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:127:in `render'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:59:in `_render_template'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:52:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:52:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:56:in `_render_template'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:26:in `render'
/Users/me/Documents/Projects/zvg2/lib/tasks/zvg.rake:450

使用url_for和link_to渲染rake模板的最佳做法是什么?我打赌,因为Rails只是更加模块化,应该有一个简单的方法吗?

提前致谢!扬

4 个答案:

答案 0 :(得分:23)

当我包含Rails.application.routes.url_helpers而不是ActionView :: Helpers :: UrlHelper时,其中一种方法可行。这适用于现在:

include Rails.application.routes.url_helpers # brings ActionDispatch::Routing::UrlFor
include ActionView::Helpers::TagHelper

av = ActionView::Base.new(Rails.root.join('app', 'views'))
av.assign({
  :regions => @regions,
  :courts_by_region => @courts_by_region,
  :cities_by_region => @cities_by_region,
  :districts_by_region => @districts_by_region
})
f = File.new(file_name, 'w')
f.puts(av.render(:template => "sitemap/index.html"))
f.close

希望这有助于他人。如果有更好的解决方案,我会感兴趣。

另外,如何从绑定中自动获取分配的哈希?

答案 1 :(得分:13)

通过这样的事情我成功了:

class Template < ActionView::Base
  include Rails.application.routes.url_helpers
  include ActionView::Helpers::TagHelper

  def default_url_options
    {host: 'yourhost.org'}
  end
end  

template = Template.new(Rails.root.join('app', 'views'))
template.assign(attendee: @attendee)
template.render(template: 'sitemap/index.html.erb')

答案 2 :(得分:7)

这对我有用(Rails 3):

class TaskActionView < ActionView::Base
  include Rails.application.routes.url_helpers
  include ApplicationHelper

  def default_url_options
     {host: 'example.com'}
  end
end

def action_view
  controller = ActionController::Base.new
  controller.request = ActionDispatch::TestRequest.new
  TaskActionView.new(Rails.root.join('app', 'views'), {}, controller)
end

action_view.render(:template =>"path/to/template")

这成功地包括了我的应用程序帮助程序,标记帮助程序和URL帮助程序。您需要将应用程序/环境主机正确放入default_url_options哈希。

答案 3 :(得分:-2)

这对我有用:

html = render_to_string(:index, layout: nil)