Where to Put Ruby Helper Methods for Rails Controllers

Where to put Ruby helper methods for Rails controllers?

You should define the method inside ApplicationController.

where to put helper methods for controllers only?

If you need to use a method in the application scope then I would suggest that you keep those methods inside the application controller and in order to use them inside views.. declare those as helper methods.

For example,

class ApplicationController < ActionController::Base
helper_method :current_user, :some_method

def current_user
@user ||= User.find_by_id(session[:user_id])
end

def some_method
end
end

Helper methods to be used in Controllers AND Views in Rails

You can put it in the controller and call:

helper_method :my_method

from the controller.

Ruby on Rails - Where to put these helper type methods

Just put that method in your application helper, wherever it is called from it will return the resulting values after processing your logic, you should not over complicate things so the good old application helper is a good place to put the common method used in views and in your controllers

Rails 6 access helper method in controller

You're missing parantheses around the I18n.t argument - it should be link_to(I18n.t('cart.check_cart_status'), acqs_url), otherwise the URL will be passed as an argument to I18.t method, not to the link_to method.

Also, if you're trying to test the assignment in the rails console, you should call it like this:

helper.link_to(I18n.t('cart.check_cart_status'), app.acqs_url)

Rails 6: Controller helper methods still in the /app/helpers folder?

In Rails 6 all helpers are included in the controllers by default and accessible through helpers. This changed from previous Rails versions, it used to be each controller included a helper with the same name of the controller by default (and didn't need to access it's method through 'helpers')

How to put helper methods and private methods in helpers

hello gates you have to include extend ActiveSupport::Concern in your concern .
This should not be in your helper folder instead pull it somewhere in you concern folder

the end file may look like

module DashboardHelper
extend ActiveSupport::Concern
module ClassMethods
def get_date(log)
end


def get_working_hours(log)
end

helper_method :get_date, :get_working_hours

private
def employee_params

end

def identify_employee
end

def check_is_arrived
end

def calculate_time_percentage
end

end
end

Calling helper method from Rails 3 controller

 view_context.some_helper_method

Ruby on Rails: Global Helper method for all controllers

You can include ApplicationHelper in your controllers (or base ApplicationController) to make the helper methods available.

You can also include the following line in your ApplicationController to include all helpers:

helper :all


Related Topics



Leave a reply



Submit