how can i use a helper in different views
Asked Answered
D

3

39

i am using refinery cms at the moment. I created an engine and with it some helpers in app/helpers/admin/. now i would like to use those helpers in my frontend view (ie. app/views/myapp/index) as well. but i can not...undefined methode error. what do i have to do short of copying the whole thing to app/helpers/? the helper looks like this

module Admin
    module myHelper
        def somefunc
        end
    end
end

so is it possible to use somefunc outside of the Admin module?

Dabster answered 20/5, 2011 at 12:2 Comment(1)
+1 good question. I struggled with this for a few minutes last night before bed. Nice to come in and find the exact question/answer I was looking for...Teacup
D
28

In your application_helper.rb:

module ApplicationHelper
  include Admin::MyHelper
end

This will import those helper methods into the ApplicationHelper, thus making them available in your views. You could do this in any of your helpers really.

Dirac answered 20/5, 2011 at 12:15 Comment(6)
Thanks that works. But why does it not work when i just include it in MyHelper...like this module MyHelper include Admin::MyHelper why do i have to go over application_helpers when it is in the same engine?Dabster
Rails doesn't include any view helper modules deeper than the top-level namespace, you need to include them yourself.Dirac
Hmm, where is my_helper.rb ?Dirac
ahh allright i found the error in my head. thanks a lot for clearing that up...have a great day!Dabster
@d11wtq: Your second comment points at the origin of the problem, could you add it to the answer? thanks.Mayle
What if I want to incude just a single method from the helper?Defunct
N
64

The "Rails way" to include a helper from a non-standard path in a view is to use the .helper method within your controller.

class MyController < ApplicationController
    helper Admin::MyHelper
    ...
end

http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper

Nanette answered 12/8, 2012 at 8:47 Comment(2)
This should be the accepted answer, much better than globally including the helper.Southport
Very nice. I was looking for this.Nip
D
28

In your application_helper.rb:

module ApplicationHelper
  include Admin::MyHelper
end

This will import those helper methods into the ApplicationHelper, thus making them available in your views. You could do this in any of your helpers really.

Dirac answered 20/5, 2011 at 12:15 Comment(6)
Thanks that works. But why does it not work when i just include it in MyHelper...like this module MyHelper include Admin::MyHelper why do i have to go over application_helpers when it is in the same engine?Dabster
Rails doesn't include any view helper modules deeper than the top-level namespace, you need to include them yourself.Dirac
Hmm, where is my_helper.rb ?Dirac
ahh allright i found the error in my head. thanks a lot for clearing that up...have a great day!Dabster
@d11wtq: Your second comment points at the origin of the problem, could you add it to the answer? thanks.Mayle
What if I want to incude just a single method from the helper?Defunct
J
0

You might try to use the full object reference like Admin::myHelper::somefunc to call somefunc from outside the Admin module.

Jedlicka answered 20/5, 2011 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.