2010-10-20 81 views
13

我似乎無法在我的Rails 3應用程序中在控制器之外呈現模板。我做的谷歌搜索很有幫助,最終我在http://www.swombat.com/rails-rendering-templates-outside-of-a-contro找到了一些有用的信息。但是,這似乎在Rails 3中被破解了。有沒有人有任何想法,我可以如何修復這種方法或者可能知道更好的方法?如何在Rails 3中的控制器之外渲染模板?

我的方法:

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 

錯誤:

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 

回答

7

我做了一個blog postgist解釋它。

基本上你需要這樣做:

class HelloWorldController < AbstractController::Base 
    include AbstractController::Rendering 
    include AbstractController::Layouts 
    include AbstractController::Helpers 
    include AbstractController::Translation 
    include AbstractController::AssetPaths 
    include ActionController::UrlWriter 

    self.view_paths = "app/views" 

    def show; end 
end 

然後:

HelloWorldController.new.show 

將返回視圖渲染爲String。

+4

你的博客文章404 – nocache 2013-05-20 07:53:03

+1

這裏是正確的博客鏈接:http://www.amberbit.com/blog/2011/12/27/render-views-and-partials-outside-controllers-in-rails- 3/ – 2014-04-09 23:23:03

+0

對於Rails 4,我必須爲ActionView :: Layouts添加一個包含:「include ActionView :: Layouts」。 – Dushyanth 2014-08-04 19:53:06

4

從Hubert建議的方法出發,我在Rails 3.1項目中結束了以下內容。

我需要渲染的部分在一個正常的視圖,並且還在資產重新使用它(在應用程序/資產/ Javascript角)

<% 
class FauxController < AbstractController::Base 
    include AbstractController::Rendering 
    # I didn't need layouts, translation or assetpaths, YMMV 
    include AbstractController::Helpers 
    include Rails.application.routes.url_helpers 
    helper MyHelper # the partial references some helpers in here 

    # Make sure we can find views: 
    self.view_paths = Rails.application.config.paths["app/views"] 

    def show 
    render :partial => "/my_controller/my_partial.js" 
    end 
end 

%> 
<%= FauxController.new.show %> 
+0

感謝那 – mhenrixon 2012-11-17 10:53:23