I'm using Rails 4.0.3. How do I render a partial from the Rails console?
我使用Rails 4.0.3。如何从Rails控制台呈现部分内容?
3 个解决方案
#1
29
Try this (in the console):
试试这个(在控制台):
# 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"
Also, for templates:
同时,模板:
av_helper.render :template => "path/to/your/template"
Update: The OP reported the partial rendering line did not work, and generated an error. I didn't encounter that, but if others do, this is the version the OP indicated was successful:
更新:OP报告了部分呈现行没有工作,并产生了一个错误。我没有遇到过这种情况,但如果其他人遇到了,这就是OP指出的成功版本:
av_helper.render :partial => 'tags/tag', :collection => Tag.limit(3)
As Josh Diehl pointed out, you can also use the usual options like locals
in the render. I would expect you should be able to use all the usual render options normally used in controllers and views.
正如Josh Diehl所指出的,您还可以使用通常的选项,比如在渲染中使用local。我希望您能够使用控制器和视图中通常使用的所有呈现选项。
Josh's example:
Josh的例子:
av_helper.render(partial: "tags/tag", locals: {term: term})
#2
8
For me the best way to get it working in Rails 4.2 was with this twoliner:
对我来说,使它在Rails 4.2中工作的最好方法是使用这个twoliner:
view = ActionView::Base.new('app/views/products', {}, ActionController::Base.new)
output = view.render(file: 'index.html', locals: {:@products => Product.all})
I found this solution on github.
我在github上找到了这个解决方案。
#3
4
There is an official way to do this in Rails 5 (cf this pull request):
在Rails 5中有一种正式的方法来实现这一点(cf请求):
ApplicationController.render 'templates/name'
程序控制器。呈现的模板/名称
The developer also made a gem to support this in Rails 4: backport_new_renderer
开发人员还制作了一个gem来支持Rails 4: backport_new_renderer
#1
29
Try this (in the console):
试试这个(在控制台):
# 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"
Also, for templates:
同时,模板:
av_helper.render :template => "path/to/your/template"
Update: The OP reported the partial rendering line did not work, and generated an error. I didn't encounter that, but if others do, this is the version the OP indicated was successful:
更新:OP报告了部分呈现行没有工作,并产生了一个错误。我没有遇到过这种情况,但如果其他人遇到了,这就是OP指出的成功版本:
av_helper.render :partial => 'tags/tag', :collection => Tag.limit(3)
As Josh Diehl pointed out, you can also use the usual options like locals
in the render. I would expect you should be able to use all the usual render options normally used in controllers and views.
正如Josh Diehl所指出的,您还可以使用通常的选项,比如在渲染中使用local。我希望您能够使用控制器和视图中通常使用的所有呈现选项。
Josh's example:
Josh的例子:
av_helper.render(partial: "tags/tag", locals: {term: term})
#2
8
For me the best way to get it working in Rails 4.2 was with this twoliner:
对我来说,使它在Rails 4.2中工作的最好方法是使用这个twoliner:
view = ActionView::Base.new('app/views/products', {}, ActionController::Base.new)
output = view.render(file: 'index.html', locals: {:@products => Product.all})
I found this solution on github.
我在github上找到了这个解决方案。
#3
4
There is an official way to do this in Rails 5 (cf this pull request):
在Rails 5中有一种正式的方法来实现这一点(cf请求):
ApplicationController.render 'templates/name'
程序控制器。呈现的模板/名称
The developer also made a gem to support this in Rails 4: backport_new_renderer
开发人员还制作了一个gem来支持Rails 4: backport_new_renderer