7

Is it possible to render a partial from inside a ruby script or from the the rails console?

How would one go about doing this?

recursive_acronym
  • 2,981
  • 6
  • 40
  • 59
  • 1
    there's a similar (and a very good) [question](http://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-rails) here on SO – Marek Příhoda Dec 16 '11 at 19:20

1 Answers1

4

Depends on the partial, what does it do, what methods it calls. But basically you have to see what templating engine it uses(erb, haml) and what calls does it make(if it calls other internal api's etc). Also if you are taking any data from the Database(using activerecord) then you will have to establish the connection to the Database yourself in the script and fetch the data.

ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => '#{YOUR_DATABSE}'

Once you establish the connection, fetch all the data that you need on your partial.

Other than that, render is pretty basic.

def render(*args, &block)
    self.response_body = render_to_string(*args, &block)
end

render_to_string, is going to call the templating engine to translate it to html. If its HAML for example would be something like:

response = Haml::Engine.new(File.read("#{partial.html.haml")).render

If your partial calls any of the rails API's you would need to copy/or include those API's and that gets complicated

daniel
  • 9,732
  • 7
  • 42
  • 57