2

I am facing the following problem: in my application I use engines. Let's say I have a shop engine. Within that shop engine I have two controllers: carts_controller and products_controller and their helpers: carts_helper and products_helper.

Now on my views/shop/products/index.html.erb view, I try to call the method cart_action which is defined in helpers/shop/carts_helper.rb. However, unfortunately I get a undefined method `cart_action' for #<#<Class:0x007fb3627af090>:0x007fb3627aab08> when I do this. When I place the same method in helpers/shop/products_helper.rb I do not get this message and the method works fine.... Why can't I use the method from carts_helper, but can I use the method from products_helper? In a normal rails app I can use any helper method on any view, right?

It might have to do something with the namespacing i.e. the helper files are not in helpers but in helpers/shop however this helps prevent conflicts with helpers from other engines or apps...

module Shop
  module CartsHelper
      def cart_action(package_id)
        #some code
      end
  end
end

How I call it on shop/products/index.html.erb:

<%= cart_action(package['id']) %>

Could it have to do with the fact that I inherit functionality for my applications_controller from my main app?:

class Shop::ApplicationController < ApplicationController
end

instead of

module Shop
  class ApplicationController < ActionController::Base
  end
end

FWIW my routes for this engine looks like:

Shop::Engine.routes.draw do
    resources :products, only: [:index]
    # shopping cart
    resource :cart, only: [:show] do
        put 'add/:package_id', to: 'carts#add', as: :add_to
        put 'remove/:package_id', to: 'carts#remove', as: :remove_from
    end

end

Thanks for any help in advance!

Note: I do not want to use my helper method in my main app, rather just on another view in the same engine.

PSR
  • 513
  • 6
  • 16

3 Answers3

3

In addition to the ApplicationHelper, only view-specific helpers are accessible to the view. Since this is your product-related view, only the ApplicationHelper + ProductsHelper are accessible. So the solution is to either move this method to ProductsHelper or to ApplicationHelper.

MarcusTres
  • 66
  • 3
  • Thanks @MarcusTres, learned something new today :) I found two other options that work for me as well, see [my answer](http://stackoverflow.com/a/29734454/3519981). – PSR Apr 19 '15 at 19:06
2

I found two ways to make the methods available to other views as well:

By creating an initializer in my /my_engine/lib/my_engine/engine.rb file as described here: https://stackoverflow.com/a/9641149/3519981

Or by including helper :all in my controllers/myengine/application_controller.rb file as described here: https://stackoverflow.com/a/1179900/3519981

Note both will make all the helpers available in the main application as well. For me that is not a problem (at the moment).

Community
  • 1
  • 1
PSR
  • 513
  • 6
  • 16
2

Not sure if anyone here has the same issue as me, but basically after I made a new custom helper and it wasn't accessible in a view, all I had to do was restart the rails server. I felt pretty silly after realizing that, well also relieved :-)

KevHo
  • 61
  • 2