Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a partial from a ruby script or console

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?

like image 609
recursive_acronym Avatar asked Dec 16 '11 18:12

recursive_acronym


People also ask

What is render partial in rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What are partials in Ruby?

Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates.

What is render in Ruby?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.


1 Answers

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

like image 64
daniel Avatar answered Oct 05 '22 23:10

daniel