Better Rails Partial Rendering

= render(@animals)
# actionview-6.1.4.1/lib/action_view/renderer/partial_renderer.rb
def find_template(path, locals)
prefixes = path.include?(?/) ? [] : @lookup_context.prefixes
@lookup_context.find_template(path, prefixes, true, locals, @details)
end
  1. If path has a / in it, then prefixes will be set to an empty array and it will search all the view directories for path. E.g. if path is lions/lion, then it will find a template like app/views/lions/_lion.
  2. Otherwise, it will traverse the controller hierarchy, e.g. if you have MeanLionsController < LionsController < ApplicationController, then a path of lion would look for a _lion partial in app/views/mean_lions/, then app/views/lions/, then finally app/views/application/.

to_partial_path

namespaces

# application.rb
config.action_view.prefix_partial_path_with_controller_namespace = false

One little problem

Un-DRY

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Clay Shentrup

Clay Shentrup

advocate of score voting and approval voting. software engineer. father. husband. american.