Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative Paths From Partials Referencing Other Partials

Tags:

I am using a (primary) partial:

<%= render partial: 'shared/page/head' %>

Which makes use of other (secondary) partials:

<head>
  <%= render partial: 'shared/page/head/title' %>
  <%= render partial: 'shared/page/head/meta' %>
  ...
  <%= render partial: 'shared/page/head/fonts' %>
  ...
  <%= render partial: 'shared/page/head/google_analytics' %>
</head>

As you can see I'm currently using paths relative to app/view for these secondary partials even though they are sitting in the same directory as the primary partial.

I've tried using relative paths:

<%= render partial: 'title' %>

Or

<%= render partial: './title' %>

But neither work.

Is there a way to have a partial resolve partials it uses using a relative path?

like image 436
Undistraction Avatar asked Oct 09 '13 10:10

Undistraction


People also ask

How do you reference a relative path?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

Does chdir work with relative path?

Yes. The current working directory is a property of the process.

How do I set a relative path in blender?

When relative paths are supported, the File Browser provides a Relative Path checkbox, when entering the path into a text field, use a double slash prefix ( // ) to make it so. Relative paths are the default but this can be changed in the File tab of the User Preferences Editor.

What is an example of a relative path?

A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.


2 Answers

This might be one solution to your problem: http://apidock.com/rails/ActionController/Base/prepend_view_path

like image 85
Lawson Kurtz Avatar answered Oct 15 '22 17:10

Lawson Kurtz


I write a helper method to implement it. Sound like work perfectly.

def render_relative_partial(relative_path, option={})
    caller_path = caller[0].split(".")[0].split("/")[0..-2].join("/")
  path = caller_path.gsub("#{Rails.root.to_s}/app/views/","") + "/#{relative_path}"

  option[:partial] = path
  render option
end 
like image 36
user1519372 Avatar answered Oct 15 '22 15:10

user1519372