views:

4627

answers:

7

I'm getting a NoMethodError when trying to access a method defined in one of my helper modules from one of my controller classes. My Rails application uses the helper class method with the :all symbol as shown below:

class ApplicationController < ActionController::Base
  helper :all
  .
  .
end

My understanding is that this should make all of my controller classes automatically include all of the helper modules within the app/helpers directory, therefore mixing in all of the methods into the controllers. Is this correct?

If I explicitly include the helper module within the controller then everything works correctly.

+1  A: 

Helpers are to be used with templates, ie. views, not in controllers. That's why you can't access the method. If you'd like to share a method between two controllers, you'd have to define it in ApplicationController, for instance. helper :all says that any method you define in any helper file in app/helpers directory will be available to any template.

Milan Novota
+7  A: 

helper :all makes all the helpers (yes, all of them) available in the views, it does not include them into the controller.

If you wish to share some code between helper and controller, which is not very desirable because helper is UI code and controller is, well, controller code. You can either include the helper in the controller or create separate module and include that in the controller and the helper also.

Honza
Thanks, that makes sense. I have some code that I want to share between two controllers. I'll create a new module and include that.
John Topley
+3  A: 

The time when I find this to be most needed is for writing the flash, or custom error checkers. Its nice to use things like link_to helpers in the flash message under some circumstances. I use the following solution to get ActionView helpers into the controller. Be advised that as was mentioned above, this breaks the MVC separation, so if anyone else has a better idea, let me know!

Below ApplicationController add this:

class Something
  include Singleton
  include ActionView::Helpers::UrlHelper
end

and inside the ApplicationController, add

def foo
  Something.instance
end

and finally, in the controller where you want to access the helper code:

messages << "<li class='error'>Your have an Error!<%= foo.link_to('Fix This', some_path) %></li>"

Hope that helps in some way!

Scott Miller
+7  A: 

Helper Methods from Controllers

One way to get at your helper methods is simply to include your helper file.

include LoginHelper
cool_login_helper_method(x,y,z)

This brings all the methods from that helper module into scope in your controller. That's not always a good thing. To keep the scope separate, create an object, imbue it with the powers of that helper, and use it to call the methods:

login_helper = Object.new.extend(LoginHelper)
login_helper.cool_login_helper_method(x,y,z)


Helper :all

helper :all makes all of your helper methods from all of your helper modules available to all of your views, but it does nothing for your controllers. This is because helper methods are designed for use in views and generally shouldn't be accessed from controllers. In newer versions of Rails, this option is always on for every controller by default.

Adrian Dunston
+5  A: 

if you need to share a method between a controller and helper/view, you can just define via 'helper_method' in the top of the controller:

class ApplicationController < ActionController::Base
  helper_method :my_shared_method
  ...

  def my_shared_method
    #do stuff
  end
end

hope that helps

ben
A: 

Any helper can be accessed using @template variable in the controller.

@template.my_super_helper

Tumtu
A: 

There are two ways to do this: either to create a module or use @template variable. Check this out for more details http://www.shanison.com/?p=305

Shanison