How to call an auxiliary method both from the point of view and from the controller in rails?

I created a helper method for some simple calculation. This helper method will simply return an integer. I need an assistant in both controllers and views.

Unfortunately, it works well in looks, but not in controllers. I get an error undefined local variable or method. How can i fix this?

Thank you all

+3
source share
2 answers

To use the same methods in both the controller and the views Add your method to application_controller.rb and make it helper methods .

for instance

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  #protect_from_forgery # See ActionController::RequestForgeryProtection for details
  helper_method :current_user 

  def current_user
    session[:user]
  end

end

current_user

+10

. , .

module TestHelper
  def my_helper_method
    #something
  end
end


class SomeController < ApplicationController
  def index
    template.my_helper_method
  end
end
+4

Source: https://habr.com/ru/post/1745256/


All Articles