2010-10-20 11 views
13

Non riesco a visualizzare un modello all'esterno di un controller nella mia applicazione Rails 3. Il googling che ho fatto è stato utile, e alla fine ho trovato alcune informazioni utili al http://www.swombat.com/rails-rendering-templates-outside-of-a-contro. Tuttavia, questo sembra essere rotto in Rails 3. Qualcuno ha qualche idea su come posso risolvere questo metodo o forse conoscere un approccio migliore?Come posso eseguire il rendering di un modello all'esterno di un controller in Rails 3?

mio metodo:

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 

L'errore:

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 

risposta

7

ho fatto un blog post e un gist che lo spiega.

In sostanza è necessario fare questo:

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 

e poi:

HelloWorldController.new.show 

vista renderizzata in una stringa tornerà.

+4

tuoi post sul blog 404s – nocache

+1

ecco il link del blog corretta: http://www.amberbit.com/blog/2011/12/27/render-views-and-partials-outside-controllers-in-rails- 3/ –

+0

Per Rails 4, ho dovuto aggiungere un include per ActionView :: Layout: "include ActionView :: Layouts". – Dushyanth

4

Sulla base dell'approccio suggerito da Hubert, ho concluso quanto segue nel mio progetto Rails 3.1.

ho bisogno di rendere un parziale vista normale e anche ri-utilizzarlo in un asset (in app/attività/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

Grazie per questo – mhenrixon

Problemi correlati