Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: How to render ERb template in rake task?
    text
    copied!<p>I am rendering a pretty huge sitemap HTML file with rake. Unfortunately, the code breaks when I migrate to rails 3. My current code looks like this:</p> <pre><code>@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)) </code></pre> <p>This code worked in 2.3, but breaks in Rails 3 as url_for does not access @controller anymore, but controller. (I think that's why.)</p> <pre><code>undefined local variable or method `controller' for #&lt;Object:0x3794c&gt; /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 </code></pre> <p>I also tried creating an ActionView to do it like that:</p> <pre><code>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 =&gt; "sitemap/index.html")) </code></pre> <p>But the problem seems to be the same, although ActionView::Base.new takes my controller.</p> <pre><code>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 </code></pre> <p>What's the best practice for rendering a template with rake that uses url_for and link_to? I bet as Rails just got more modular, there should be an easy way for just this?</p> <p>Thanks in advance! Jan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload