Rails 3: Jak renderować szablon ERb w zadaniu rake?

Renderuję całkiem ogromny plik HTML mapy witryny za pomocą rake. Niestety, kod pęka podczas migracji do rails 3. Mój obecny kod wygląda tak:

@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))

Ten kod działał w 2.3, ale łamie się w Rails 3, ponieważ url_for nie ma już dostępu do @ controller, ale controller. (Myślę, że dlatego.)

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

Próbowałem również stworzyć ActionView, aby zrobić to w ten sposób:

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"))

Ale problem wydaje się być ten sam, chociaż Actionview:: Base.new takes my kontroler.

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

Jaka jest najlepsza praktyka renderowania szablonu z rake ' em, który używa url_for i link_to? Założę się, że skoro Rails stał się bardziej modułowy, powinien być na to łatwy sposób?

Z góry dzięki! Jan
Author: Jan, 2010-11-24

4 answers

Jedno z podejść działa, gdy włączam Rails.podanie.trasy.url_helpers zamiast ActionView:: Helpers:: UrlHelper. Na razie działa:

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
Mam nadzieję, że to pomoże innym. Jeśli jest lepsze rozwiązanie, byłbym zainteresowany.

Ponadto, jak automatycznie uzyskać hash przypisań z wiązania?

 23
Author: Jan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-24 09:14:24

Udało mi się zrobić coś takiego:

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')
 13
Author: Overbryd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-23 23:06:52

To mi się udało (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")

To z powodzeniem obejmował mój Pomocnik aplikacji, pomocników tagów i pomocników url. Będziesz chciał poprawnie umieścić swój host aplikacji/środowisk w haszu default_url_options.

 7
Author: Josh Dzielak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-21 08:58:52

To mi się udało:

html = render_to_string(:index, layout: nil)
 -2
Author: Constantine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-26 13:18:29