Skip to content

Instantly share code, notes, and snippets.

@agungyuliaji
Forked from aliang/mymodel.rb
Created May 2, 2014 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agungyuliaji/1f1538400347368726fa to your computer and use it in GitHub Desktop.
Save agungyuliaji/1f1538400347368726fa to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment