Pau*_*ter 37
试试这个(在控制台中):
# initial setup
view_paths = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
av_helper = ActionView::Base.new view_paths
# (Optional) include this if your partial uses route helpers:
include Rails.application.routes.url_helpers
av_helper.render "path/to/your/partial"
Run Code Online (Sandbox Code Playgroud)
另外,对于模板:
av_helper.render :template => "path/to/your/template"
Run Code Online (Sandbox Code Playgroud)
更新: OP报告部分渲染行不起作用,并生成错误.我没有遇到过这种情况,但如果其他人这样做,这就是OP表示成功的版本:
av_helper.render :partial => 'tags/tag', :collection => Tag.limit(3)
Run Code Online (Sandbox Code Playgroud)
正如Josh Diehl指出的那样,你也可以使用locals渲染中的常用选项.我希望你能够使用控制器和视图中通常使用的所有常用渲染选项.
乔希的例子:
av_helper.render(partial: "tags/tag", locals: {term: term})
Run Code Online (Sandbox Code Playgroud)
Nic*_*vre 15
在Rails 5中有一种正式的方法(参见此拉取请求):
ApplicationController.render 'templates/name'
开发人员还在Rails 4中创建了一个gem来支持它:backport_new_renderer
Fab*_*ian 10
对我来说,让它在Rails 4.2中工作的最好方法是使用这个twoliner:
view = ActionView::Base.new('app/views/products', {}, ActionController::Base.new)
output = view.render(file: 'index.html', locals: {:@products => Product.all})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10965 次 |
| 最近记录: |