Nona
Nona

Reputation: 5462

How to add a custom view partial path to Rails 4.2 lookup?

I have a demonstration partial in app/views/app1/users similar to this this solution. I've already tried using the prepend_view_path as in the aforementioned solution, to no avail.

haml:

#demonstration
= f.semantic_fields_for :demonstration do |ud|
  = render 'demonstration_fields', :f => ud

But I get this error:

Missing partial app1/_demonstration_fields, application/_demonstration_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}. Searched in:

The only way to make it work is to pass the full path to render as follows:

= render 'app1/users/demonstration_fields', :f => ud

But this defeats the purpose of trying to avoid redundant code (e.g., specifying a full path) via prepend_view_path. Is there a way to avoid passing in the full path?

Upvotes: 4

Views: 1142

Answers (1)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

You could override the ActionView::ViewPaths.local_prefixes private class method that every controller in Rails gets mixed in. The comment above the method even says:

Override this method in your controller if you want to change paths prefixes for finding views

"Views", in this context, would also mean partials. So, you could add the following to your App1Controller:

class App1Controller < ApplicationController
  def self.local_prefixes
    [controller_path, "#{controller_path}/users"]
  end
  private_class_method :local_prefixes
end

Then, when you render 'demonstration_fields', :f => ud, your lookup path should look like (in order):

app1/_demonstration_fields,
app1/users/_demonstration_fields,
application/_demonstration_fields`

Upvotes: 3

Related Questions