NoMethodError when trying to invoke helper method from Rails controller
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.
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 a separate module and include that in the controller and the helper as well.
Using helper methods inside your controllers, 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� ruby on rails - NoMethodError when trying to invoke helper method from Rails controller ruby helper method (9) Helper Methods from Controllers One way to get at your helper methods is simply to include your helper file.
hepler :all or helper SomeHelper not worked in ActionController::API , If you included the helper module inside the controller, as I did many times The first thing I tried was to invoke the helper method directly from the action method: NoMethodError: undefined method `sanitize' for #<SomeController> If you use the include approach in newer Rails applications I strongly� If I've got a method in a different controller to the one I'm writing in, and I want to call that method, is it possible, or should I consider moving that method to a helper? I don't know any details of your problem, but maybe paths could be solution in your case (especially if its RESTful action).
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
How to Use Rails Helpers (Complete Guide), AFAIK: If Rails API skip helpers, when I invoke helper method, is should told me, use helpers.display_price method here, not exist # NoMethodError: XxxApi:: V1::ApiController helper CommonHelper def index # try use� If you want to use helpers outside of views you’ll need something else. How to Use Helpers From Controllers. It’s possible, although not very common, to use helper methods from controller actions. Before Rails 5, you had to include the helper module. In newer versions, you can use helpers in your controller with the helpers (plural) object
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 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.
Undefined method error for helper method on view in rails Engine, A helper is a method that is (mostly) used in your Rails views to share reusable code. Later you'll learn how to use them in controllers & why that may be a bad idea. I love using the Rails console (irb with your Rails app loaded) to try out methods NoMethodError: undefined method number_to_human' for main: Object Method not defined by link_to helper I need to post through a link tag, however there is a problem with the link helper. Here is my link tag // doctors.html.erb <%= link_to "Ekle", [:add_doctor, @patient], method: :post %> And my routes.rb // routes.rb get 'patients/:id/
For Rails 3, use the
view_context method in your controller:
def foo view_context.helper_method ...
Now on my views/shop/products/index.html.erb view, I try to call the method method `helper_method' for ActionController::API:Class (NoMethodError) #3690 � Strange thing – I have Authentication module in lib/ like this: module Authentication protected def current_user User.find(1) end end and in ApplicationController I'm including this m
It only uses the session if you haven't excluded your strategy from session storage. See lib/devise.rb and look for skip_session_storage.Token auth by default has it enabled to use session storage because one of the use
Hello, I hope everyone is having a great holiday. I'm using rails 4.2.1 and ruby 2.2.1, I have paperclip 4.3.2 installed. I'm wondering if anyone can help with a solution to the following issue.
So I have a Rails engine contained within a Rails app. I want to do something like redirect_to @record.polymorphic_relationship in one of my engine's controllers, where @record.polymorphic_relation
- Is the controller in question inheriting from ApplicationController?
- Yes. I get the problem in two controllers that use the method in question and both controller inherit from ApplicationController.
- 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.
- That's the best approach indeed
- Is it really about sharing between two controllers? Or between controller and view? I think the latter. So gamecreature's way (@template, view_context) looks correct to me.
- My funky redirect methods are in helpers. I need them from my controllers. I don't think this is bad.
- You say it's undesirable to include helper methods in the controller because UI code and controller code should be separate. But what if you are doing filtering and you obviously need to access the params hash in the controller and view and sometimes you need to call methods in the view and controller to access params variables. So what would you do in that circumstance?
- This is the best answer of the bunch - should be at the top
- For Rails 3. THIS IS THE ANSWER.
- No explanation for how to use
- Another solution (works in both rails versions) is to use: ApplicationController.helpers.number_to_currency( 42.0)
- The line
helper_method :my_shared_methoddoes not need to be at "the top".. it can go anywhere, and I like it best immediately above the definition (