4

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?

Community
  • 1
  • 1
Nona
  • 5,302
  • 7
  • 41
  • 79
  • Possible duplicate of [How to prepend rails view paths in rails 3.2 (ActionView::PathSet)](http://stackoverflow.com/questions/10864108/how-to-prepend-rails-view-paths-in-rails-3-2-actionviewpathset) – Brad Werth Oct 07 '15 at 22:35
  • So why not move the demonstration field to a path that works? Such as a shared folder? `shared/users/demonstration_fields` – miler350 Oct 08 '15 at 04:03
  • @miler350, in my specific case I'm actually using year folders such as "users_2014" and want to keep partials segmented by year - it's a very specific setup. – Nona Oct 08 '15 at 05:02
  • try this line [= render partial: "app1/users/demonstration_fields"] – Hemanth M C Oct 08 '15 at 07:04

1 Answers1

3

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`
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122