从 ruby​​ 脚本或控制台渲染部分 [英] Rendering a partial from a ruby script or console

查看:32
本文介绍了从 ruby​​ 脚本或控制台渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 ruby​​ 脚本内部或从 rails 控制台渲染部分?

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

人们会怎么做呢?

推荐答案

取决于部分,它做什么,它调用什么方法.但基本上你必须看看它使用什么模板引擎(erb,haml)以及它做了什么调用(如果它调用其他内部api等).此外,如果您从数据库中获取任何数据(使用 activerecord),那么您必须自己在脚本中建立与数据库的连接并获取数据.

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,将调用模板引擎将其转换为 html.例如,如果它的 HAML 类似于:

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

如果您的部分调用了任何 rails API,您将需要复制/或包含这些 API,这会变得复杂

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

这篇关于从 ruby​​ 脚本或控制台渲染部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆