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?


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

链接地址: http://www.djcxy.com/p/56740.html

上一篇: Sweave无法加载R软件包

下一篇: 从ruby脚本或控制台渲染部分内容