How to Call Applicationcontroller Methods from Applicationhelper

How to call ApplicationController methods from ApplicationHelper

Use helper_method.

By default, methods in ApplicationController are only accessible inside the Controllers.

Add a method to the ApplicationController and expose it as a helper method with helper_method:

class ApplicationController < ActionController::Base

helper_method :foo

def foo
"bar"
end

end

Now the foo method is accessible to both Controllers and Views.

What's the best way to call an application helper from a controller?

You can do this by either including your helper module in the controller, or you can use module_function to declare your helper method as callable directly on the module, sort of like a class method.

Say that your helper function is declared in this helper module:

module ApplicationHelper
def format_name(user_id)
# do stuff
end
module_function :format_name # Here's the "magic" line
end

By adding that module_function :format_name line, you can now call your helper function just like ApplicationHelper.format_name(user[:id]) in the controller.

Calling helper method from Rails 3 controller

 view_context.some_helper_method

How to access ApplicationHelper methods in namespaced views?

You didn't include your controller code, but we'll assume it ultimately inherits from ActionController::API (as it should it if it is an API controller). If so, that is the root of it rather than namespacing, etc. Per the ActionController documentation:

An API Controller is different from a normal controller in the sense
that by default it doesn't include a number of features that are
usually required by browser access only: layouts and templates
rendering, flash, assets, and so on. This makes the entire controller
stack thinner, suitable for API applications. It doesn't mean you
won't have such features if you need them: they're all available for
you to include in your application, they're just not part of the
default API controller stack.

One of the side effects of the thinner API controller is that they don't automatically include helpers like a standard Rails controller. You can easily add that back in, though.

messages_controller.rb

class Api::V1::MessagesController < ActionController::API
include ActionController::Helpers
helper ApplicationHelper

def show
# whatever
end
end

app/helpers/application_helper.rb

module MessagesHelper
def some_method(arg)
# whatever
end
end

app/views/messages/show.json.jbuilder

json.bogus do
thing1: some_method('banana')
end

If you have lots of API controllers, you can of course stick it in a base controller class they all inherit from like so:

class Api::V1::ApiController < ActionController::API
include ActionController::Helpers
helper ApplicationHelper
end

undefined method when calling helper method from controller in Rails

You cannot call helpers from controllers. Your best bet is to create the method in ApplicationController if it needs to be used in multiple controllers.

EDIT: to be clear, I think a lot of the confusion (correct me if I'm wrong) stems from the helper :all call. helper :all really just includes all of your helpers for use under any controller on the view side. In much earlier versions of Rails, the namespacing of the helpers determined which controllers' views could use the helpers.

I hope this helps.

How to call methods defined in ApplicationController in models

Why would you need such thing? The model should not know about its controllers. Maybe a redesign of your system will be more appropriate in this case.

Here is a link to similar thread.

Rails - How to use a Helper Inside a Controller

Note: This was written and accepted back in the Rails 2 days; nowadays grosser's answer is the way to go.

Option 1: Probably the simplest way is to include your helper module in your controller:

class MyController < ApplicationController
include MyHelper

def xxxx
@comments = []
Comment.find_each do |comment|
@comments << {:id => comment.id, :html => html_format(comment.content)}
end
end
end

Option 2: Or you can declare the helper method as a class function, and use it like so:

MyHelper.html_format(comment.content)

If you want to be able to use it as both an instance function and a class function, you can declare both versions in your helper:

module MyHelper
def self.html_format(str)
process(str)
end

def html_format(str)
MyHelper.html_format(str)
end
end


Related Topics



Leave a reply



Submit