0

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

Lars P
  • 796
  • 6
  • 16

2 Answers2

0

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

Community
  • 1
  • 1
Dan Rubio
  • 4,709
  • 10
  • 49
  • 106
  • Thanks for the response! The partial _is_ in `app/views`, and is rendered fine from a view. My problem is when trying to render the same partial from Helper code. – Lars P Dec 14 '16 at 05:20
  • Ok, so let me ask you this. What are you trying to accomplish with the code? What you are trying to do which is to render a partial from a the `app/helpers` directory is incorrect. The helper directory's purpose is not to render partials. That responsibility falls into the `app/views` directory. Helpers are used to extract view logic (eg. if/else .each loop) to make the views cleaner. Maybe if you can elaborate more I can help you out. I really think you may be approaching this incorrectly. – Dan Rubio Dec 14 '16 at 15:52
  • Just to supplement my comment here is a direct quote from tutorials point: `"The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the model, view, and controller code small, focused, and uncluttered."` – Dan Rubio Dec 14 '16 at 15:53
  • I think you may be misunderstanding the question. The partial is in `app/views/positions/_cube_icon.html.haml`, as it should be. What's in the helper is this code: `ActionController::Base.helpers.render partial: 'cube_icon'` I've never rendered partials from helpers before, but googling shows many examples of people doing it successfully. – Lars P Dec 14 '16 at 16:52
0

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.

Lars P
  • 796
  • 6
  • 16