rails: rails 4: rendering template outside of controller raises exception in production

hello,

I want to render a template into a file in a rake task.

this was my old code (worked with rails 3):

controller = ActionController::Base.new
view = ActionView::Base.new('app/views', {}, controller)
view.render(file: 'index')

in rails 4 (in production environment) that stopped working:

ActionView::Template::Error: undefined method `parameters' for nil:NilClass
from .../actionpack-4.0.0/lib/action_controller/metal/strong_parameters.rb:510:in `params'

then I thought it would be good to simplify my code:

ActionView::Base.new('app/views').render(file: 'index')

but this does not render asset pipeline file paths correctly.

so this is my current workaround:

ActionController::Base.new.tap { |controller|
  def controller.params
    {} # suppress strong parameters exception
  end
}.render_to_string(file: filename)

is this a rails bug or I’m doing something wrong?

thanks!

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

@pseidemann We tried this just now with master and seems to be working fine. See below for output

2.1.2 :001 > controller = ActionController::Base.new
 => #<ActionController::Base:0x0000010878f2b8 @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
2.1.2 :002 > view = ActionView::Base.new('app/views', {}, controller)
 => #<ActionView::Base:0x000001087c0458 @_config={}, @view_renderer=#<ActionView::Renderer:0x0000010880bb10 @lookup_context=#<ActionView::LookupContext:0x000001087c0408 @details_key=nil, @details={:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}, @skip_default_locale=false, @cache=true, @prefixes=["action_controller/base"], @rendered_format=nil, @view_paths=#<ActionView::PathSet:0x000001087c0340 @paths=[#<ActionView::OptimizedFileSystemResolver:0x000001087c02f0 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x000001087c02c8 @data=#<ActionView::Resolver::Cache::SmallCache:0x000001087c02a0 @backend={}, @default_proc=#<Proc:0x00000103b77808@/Users/arunagw/checkouts/rails/actionview/lib/action_view/template/resolver.rb:48 (lambda)>>>, @path="/Users/arunagw/crap/master/foobar/app/views">]>>>, @_assigns={}, @_controller=#<ActionController::Base:0x0000010878f2b8 @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil, @_config={}>, @_request=nil, @view_flow=#<ActionView::OutputFlow:0x0000010880b9f8 @content={}>, @output_buffer=nil, @virtual_path=nil>
2.1.2 :003 > view.render(file: 'index')
  Rendered index.html.erb (0.7ms)
 => "Hello\n"

Closing this for now. Please let me know if you see this with latest version of Rails.

cc/ @m1k3