ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年2月8日 星期一
render outside your controller
Rails: Calling render() outside your Controllers
Calling Rails Render Partial in a Model or Background Task
# lib/render_anywhere.rb
#
# Render templates outside of a controller or view.
#
# Simply mixin this module to your existing class, for example:
#
# class MyTemplater < ActiveRecord::Base
# include RenderAnywhere
#
# And you can use render() method that works the same as ActionView::Base#render
#
# obj = MyTemplater.new
# obj.html = obj.render :file => '/shared/header'
#
#
module RenderAnywhere
class DummyController
def logger
RAILS_DEFAULT_LOGGER
end
def headers
{}
end
end
def render(options, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.render options
end
def template_exists?(path, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.pick_template_extension(path) rescue false
end
end
class Page < ActiveRecord::Base
def write_cache
self.cached_content = ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial => "pages/show", :locals => {:page => self})
self.save
end
end
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言