3

How do I render a partial that's in a different directory (in my case, the parent) than the current view?

<%=render_partial :sidebar%> #looks in the current dir and works as expected
<%=render_partial "/view/sidebar"%> #doesn't work!

Thanks!

Sunder
  • 1,445
  • 2
  • 12
  • 22

3 Answers3

1

You have to specify the right controller that is responsible for the right view:

TheRightController.render_partial :sidebar

If you don't specify the controller class, render_* works for the current action (controller) only, except render_full that does real internal HTTP request.

So, the answer is: If you need shared templates, just create special controller, i.e. called Shared, without any action methods inside, just with many templates in an appropriate view folder and call Shared.render_partial.

Shared.render_partial works like internal request. It renders contents of the controller's action and even the action's method is executed. If you want to render just the view (without executing Shared's action method), use Shared.render_view instead.

Moreover, you can use the internal requesting to prepare some data in the Shared controller's method. For instance, if your sidebar consists of @articles, let's load them in the Shared's sidebar() method. You don't need to load @articles in any other controller that displays the sidebar! You only call "Shared.render_partial :sidebar" in there. This is how to build widget-like web with Ramaze :-)

Jan Molič
  • 21
  • 1
-1

I found following api in apidock.com, maybe useful for u

 # Renders a collection of partials located in a view subfolder
 # outside of our current controller.  In this example we will be
 # rendering app/views/shared/_note.r(html|xml)  Inside the partial
 # each element of @new_notes is available as the local var "note".
 render :partial => "shared/note", :collection => @new_notes
Richie Min
  • 654
  • 5
  • 15
  • Thank you Richie but I'm afraid that doesn't work. Ramaze doesn't have a render method in the view (it does have a few render_* methods); the code sample above might be Rails? – Sunder Dec 07 '12 at 02:13
  • oh, sorry, can't see the tag clearly, I think u can use render_file instead, it can include the full path. – Richie Min Dec 07 '12 at 02:18
  • Yes, there's always that option, but I'd rather not use an absolute path. – Sunder Dec 07 '12 at 02:23
  • ramaze is same as Rails, render relative path is only render partial in the same view folder, if u want render another folder view partial, then u shoud render partial with the full view path, unless the framework cann't found the view partial. – Richie Min Dec 07 '12 at 02:35
  • Ah, I see what's going on. render_file doesn't seem to auto-detect the extension. So, this works -- <%=render_file "view/partials/mnu.erb"%>. Thanks for your help. – Sunder Dec 07 '12 at 02:37
-1

@rebnoob may use (without view directory name, because Rails search on app/view directory)

<%= render "/sidebar" %>

instead of

<%=render_partial "/view/sidebar"%> #doesn't work!

seyyah
  • 97
  • 1
  • 5