Ruby on rails 3 Rails 3:如何在rake任务中呈现ERb模板?
我正在用rake呈现一个相当大的站点地图HTML文件。不幸的是,当我迁移到rails 3时,代码中断了。我当前的代码如下所示:Ruby on rails 3 Rails 3:如何在rake任务中呈现ERb模板?,ruby-on-rails-3,rake,erb,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
@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))
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.3中运行,但在Rails 3中中断,因为url_不再访问@controller,而是访问controller。(我想这就是原因。)
但问题似乎是一样的,尽管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和链接的rake呈现模板的最佳实践是什么?我敢打赌,由于Rails刚刚变得更加模块化,应该有一种简单的方法来实现这一点
提前谢谢!Jan当我包含Rails.application.routes.url\u 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
希望这能帮助别人。如果有更好的解决办法,我会感兴趣的
另外,如何从绑定中自动获取分配的哈希值?我成功地执行了以下操作:
@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))
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')
类模板
这就是我的工作原理(Rails 3):
class TaskActionView“path/to/template”)
这成功地包括了我的应用程序帮助程序、标记帮助程序和url帮助程序。您需要将您的应用程序/环境主机正确地放入默认的\u url\u options散列中。这对我很有用:
html = render_to_string(:index, layout: nil)
谢谢你,乔希!!!您的ActionController和ActionDispatch解决方案在生产环境和嵌套部分中运行良好,而其他解决方案仅在开发环境中运行。一件事是,RubyonRails 3.2.x的模板名称中不需要
.html.erb
。使用.html.erb
它会产生弃用警告,并要求使用:handlers
和:formats
这在rails 3.2上适用吗?我在控制台中尝试了这个未定义的方法。
html = render_to_string(:index, layout: nil)