NoMethodError when trying to call a helper method from a Rails controller

I get a NoMethodError when I try to access a method defined in one of my helper modules from one of my controller classes. My Rails uses the helper class method with the symbol :all , as shown below:

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

I understand that this should force all my controller classes to automatically include all auxiliary modules in the app / helpers directory, so mixing all the methods in the controllers. Is it correct?

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

+72
ruby ruby-on-rails
Jan 17 '09 at 18:18
source share
14 answers

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

If you want to share some kind of code between the helper and the controller, which is not very desirable, because the auxiliary UI code and the controller, well, the controller code, you can either include an assistant in the controller, or create a separate module and include this in the controller and assistant.

+49
Jan 17 '09 at 19:19
source share
— -

To use helper methods already included in the template engine:

An example of using the call to 'number_to_currency' in the controller method:

 # rails 3 sample def controller_action @price = view_context.number_to_currency( 42.0 ) end # rails 2 sample def controller_action @price = @template.number_to_currency( 42.0 ) end 
+133
Dec 02 '10 at 19:51
source share

if you need to split the method between the controller and the helper / view, you can simply define via "helper_method" at 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

+30
Jan 20
source share

Helper Methods from Controllers

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

 include LoginHelper cool_login_helper_method(x,y,z) 

Brings all methods from this auxiliary module to the area of ​​your controller. This is not always good. To divide an area, create an object, fill it with the authority of this assistant and use it to call methods:

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



Assistant: 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 to your controllers. This is because helper methods are intended for use in views and, as a rule, should not be accessible from controllers. In newer versions of Rails, this option is always enabled for each controller by default.

+28
Jan 26 '09 at 22:08
source share

For Rails 3, use the view_context method in your controller:

 def foo view_context.helper_method ... 

Here is an example: http://www.christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes/

+7
Sep 10 '12 at 18:03
source share

The time when I find it most necessary is to write flash memory or custom error checks. In some cases, it's nice to use things like link_to helpers in a flash message. I use the following solution to get ActionView helpers in the controller. Keep in mind that, as 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 ApplicationController add

 def foo Something.instance end 

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

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

Hope this helps in some way!

+5
Jan 17 '09 at 21:11
source share

Probably a cleaner helpers method:

 class FooController < ActionController::Base def action self.class.helpers.helper_method arg end end 
+5
Jul 11 2018-11-11T00:
source share

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

@ template.my_super_helper

+4
May 6 '10 at 16:54
source share

The controller cannot automatically access helper methods. We must include them in the application controller.

module ApplicationHelper

  def hello_message "Hello World" end 

end

class ApplicationController <ActionController :: Base

  include ApplicationHelper def message hello_message end 

end

+3
Mar 02 '14 at 6:10
source share

Helpers should be used with templates, i.e. not in controllers. That is why you cannot access the method. If you want to share a method between two controllers, you must define it in the ApplicationController, for example. helper: everyone says that any method that you define in any helper file in the app / helpers directory will be available for any template.

+2
Jan 17 '09 at 19:19
source share

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

+1
Jun 02 2018-10-06T00:
source share

If you only have an ApplicationHelper inside your app/helpers , you should load it into your controller using the include ApplicationHelper . By default, Rails only loads a helper module with the same name as your controller. (e.g. ArticlesController will load ArticlesHelper). If you have many models (for example, "Articles, posts", "Categories"), you must download each of them in your controller. documents

Assistant

 module PostsHelper def find_category(number) return 'kayak-#{number}' end def find_other_sport(number) "basketball" #specifying 'return' is optional in ruby end end module ApplicationHelper def check_this_sentence 'hello world' end end 

Controller example

 class ArticlesController < ApplicationController include ApplicationHelper include PostsHelper #...and so on... def show#rails 4.1.5 #here I'm using the helper from PostsHelper to use in a Breadcrumb for the view add_breadcrumb find_other_sport(@articles.type_activite), articles_path, :title => "Back to the Index" #add_breadcrumb is from a gem ... respond_with(@articles) end end 
0
Dec 16 '14 at 7:52
source share

If you change the application_controller.rb file to this ...

 class ApplicationController < ActionController::Base protect_from_forgery with: :exception include SessionsHelper end 

... then all assistants will be available to all controllers.

0
Mar 03 '17 at 10:32
source share

Rails 5 update. I ran into a similar problem with views not picking a method from application_helper.rb. This post has helped me. From what I can compile, the files that are provided in the assistant folder are for these views only. Methods in application_helper will not be automatically available for all views. To create a helper method accessible to all views, you need to create a new helper file in the helpers directory, for example clean_emails.rb and add your custom method here.

 Module CleanEmails def clean_email(email) *do some stuff to email* return email end end 

You can then call <%= clean_email(email) %> from any view in your application.

0
May 20 '19 at 17:33
source share



All Articles