render doesn't know where to look for partials (from helper code)

271 views Asked by At

When I try to render a partial from a helper, it fails with this (condensed) error message:

Missing partial /_cube_icon with [...]. Searched in:

Note that the list of searched directories is empty!

In contrast, when using render in a view, it knows where to look:

Searched in:  * "/Users/Lars/GitHub/algdb/app/views"

In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?

This is Rails 4.2.4 ยท Ruby 2.3.1

2

There are 2 answers

4
Dan Rubio On

Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering

Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.

---Updated-----

If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:

In app/helpers

 module MyHelper
   def custom_render
     concat(render(:partial => 'cube_icon'))
   end
 end

Here are some links from stackoverflow to help you out.

Rendering a partial from helper #1

Rendering from a partial from herlper #2

Using concat rails

1
Lars P On

OK, I figured it out:

I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.

When following that convention, things started working.