且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 ruby​​ 脚本或控制台渲染部分

更新时间:2023-12-05 18:11:46

取决于部分,它做什么,它调用什么方法.但基本上你必须看看它使用什么模板引擎(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