How do I render a partial from the Rails console?
ruby, ruby-on-rails, ruby-on-rails-4
Solution
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:
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's example:
av_helper.render(partial: "tags/tag", locals: {term: term})
Problem
I'm using Rails 4.0.3. How do I render a partial from the Rails console?