ApplicationHelper not loaded in Rails 4 engine

Long time SO reader here. I am working on Rails Engine. The problem with the big picture is that I am getting a NoMethodError for a helper method that lives in my Engine ApplicationHelper. This is for work, so I will call the engine Blorge

.

I have a helper method of mine that causes problems anywhere it is called. The Helper method returns NoMethodError. I thought I needed to manually add helper Blorge::ApplicationHelper

to Blorge :: ApplicationController, but the problem still happens.

Am I missing something principled about Motors here?

Here's some actual code to give you a better idea of ​​what I'm looking for.

index_header partial

app/views/blorge/shared/_index_header.html.erb

# require_locals is the helper method in question here
<% require_locals ['title'], local_assigns %>
<% title = title.pluralize %>

<section class="main_content-header">
  <div class="main_content-header-wrapper">
    <%= content_tag :h1, title %>

    <div class="main_content-header-save">
      <%= link_to "New #{title.singularize}", @new_path, class: "add-button" %>
    </div>
  </div>
</section>

      

Pages # home view

app/views/blorge/pages/home.html.erb

<%= render 'blorge/shared/index_header', title: "Welcome, #{current_user.full_name}" %>

...

      

Engine Use Assistant

app/helpers/blorge/application_helper.rb

module Blorge
  module ApplicationHelper

    def require_locals(local_array, local_assigns)
      local_array.each do |loc|
        raise "#{loc} is a required local, please define it when you render this partial" unless local_assigns[loc.to_sym].present?
      end
    end

  end
end

      

Engine page controller

app/controller/blorge/pages_controller.rb

module Blorge
  class PagesController < ApplicationController

    def home
    end

  end
end

      

Engine control controller

app/controllers/blorge/application_controller.rb

class Blorge::ApplicationController < ActionController::Base
  helper Blorge::ApplicationHelper

  ...
end

      

If I restart the server and reload the page, it usually works fine, and once it crashes, the problem doesn't return for a couple of days. After reading Helpers in Rails Engine and Rails Engines: Helpers are only reloaded when the server is restarted. It sounds like I need to include a helper in my application controller using a method to_prepare

in my file engine.rb

. I'm going to try this further, but what I most want to know is if something very basic is missing here. If I just need to add it to engine.rb, can someone explain why?

It may have been too much information, but I would rather give more than not enough. Thank you in advance.


Edit

It looks like the fix is ​​adding helpers to the app controller within engine.rb

. I suspected this would be a fix, but I still don't know why it is. Does anyone know why I need this?

Decision

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end

      

+3


source to share





All Articles