Parece que no puedo renderizar una plantilla fuera de un controlador en mi aplicación Rails 3. La búsqueda en Google que he hecho ha sido útil, y finalmente encontré información útil al http://www.swombat.com/rails-rendering-templates-outside-of-a-contro. Sin embargo, esto parece estar roto en Rails 3. ¿Alguien tiene alguna idea de cómo puedo solucionar este método o tal vez conocer un mejor enfoque?¿Cómo puedo representar una plantilla fuera de un controlador en Rails 3?
Mi método:
def render_erb(template_path, params)
view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << view
include ApplicationHelper
end
view.render(:file => "#{template_path}.html.erb", :locals => params)
end
El error:
ActionView::Template::Error: ActionView::Template::Error
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /Users/mikegerstenblatt/Desktop/bluetrain/lib/test.html.erb:17:in `_lib_test_html_erb__366962844_2173671680_68830'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `send'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `render'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:54:in `instrument'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:127:in `render'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:59:in `_render_template'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:56:in `_render_template'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:26:in `render'
from /Users/mikegerstenblatt/Desktop/bluetrain/app/models/generated_website.rb:45:in `render_erb'
from (irb):2
tu publicación de blog 404s – nocache
Aquí está el enlace correcto del blog: http://www.amberbit.com/blog/2011/12/27/render-views-and-partials-outside-controllers-in-rails- 3/ –
Para Rails 4, tuve que agregar una inclusión para ActionView :: Layouts: "incluir ActionView :: Layouts". – Dushyanth