Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
:partial => 'my_models/my_model', :format => :txt,
:locals => { :my_model => self}
)
end
end
@mikepence

This comment has been minimized.

Copy link

mikepence commented Dec 20, 2012

    ActionView::Base.new(
        Rails.configuration.paths["app/views"]).render(
        :partial => 'componenets/hello', :format => :txt, 
        :locals => { :my_model => self})
@jpwynn

This comment has been minimized.

Copy link

jpwynn commented May 5, 2013

thanks also for that update @mikepence, very helpful.

Perfectly legit reasons to do this if, for example, you are having a delayed job send a mass email whose view your want to render using the data values at the time the emails are sent, NOT when the controller action queues the delayed job. (if it's a mass email, you do not want the emailer to have to re-render the same view 1000 times, so you have the MODEL method that the delayed job invokes do the rendering).

@mongrelion

This comment has been minimized.

Copy link

mongrelion commented May 27, 2013

This doesn't work if you're using url helpers (root_path, foo_path, bar_url and such) in the view. Any thoughts on how to get it working?

@nathanbertram

This comment has been minimized.

Copy link

nathanbertram commented Jun 20, 2013

any luck @mongrelion ? Having teh same problem with url helpers

@nathanbertram

This comment has been minimized.

Copy link

nathanbertram commented Jun 20, 2013

@mongrelion figured it out just do:

    action_view = ActionView::Base.new(Rails.configuration.paths["app/views"].first)
    action_view.class_eval do 
        include Rails.application.routes.url_helpers
    end
@nathanbertram

This comment has been minimized.

Copy link

nathanbertram commented Jun 20, 2013

@mongrelion final solution for me:

   action_view = ActionView::Base.new(Rails.configuration.paths["app/views"])
    action_view.class_eval do 
        include Rails.application.routes.url_helpers
        include ApplicationHelper

        def protect_against_forgery?
          false
        end
    end
@mark-ellul

This comment has been minimized.

Copy link

mark-ellul commented Jun 27, 2013

THANK YOU, THANK YOU AND THANK YOU... I had tried many different variations and this is the first one that worked!

@KarlGl

This comment has been minimized.

Copy link

KarlGl commented Feb 5, 2014

@nathanbertram Thank you.

@agungyuliaji

This comment has been minimized.

Copy link

agungyuliaji commented May 2, 2014

Thanks Man! Very Help Me

@manoj2411

This comment has been minimized.

Copy link

manoj2411 commented Jun 20, 2014

Thanks buddy

@patrickdavey

This comment has been minimized.

Copy link

patrickdavey commented Jun 24, 2015

I was doing this recently for injecting a view_context for an Rspec test. I was having issues with the URL helpers even though the default_url_options were set in config/environments/test.rb

ended up adding to the class_eval

action_view.class_eval do 
  include Rails.application.routes.url_helpers
  def default_url_options
    { :host => "example.com" }
  end
  # ...
end

Worked for me.. might possibly help others :)

@zdravko

This comment has been minimized.

Copy link

zdravko commented Aug 12, 2016

Excellent! Thank you!
I wonder, though, how fast is this, since we know that rails rendering a view can be slow.
Is there a way to embed this model rendering via helper?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.