Where are Rails helpers available?
In Rails 5, all helpers are available for all views and all controllers, and nothing else.
http://api.rubyonrails.org/classes/ActionController/Helpers.html
These helpers are available for all default templates.
By default, each controller will include all helpers.
In views, you can access helpers directly:
module UserHelper
def fullname(user)
...
end
end
# app/views/items/show.html.erb
...
User: <%= fullname(@user) %>
...
In controllers, you need a method #helpers
to access them:
# app/controllers/items_controller.rb
class ItemsController
def show
...
@user_fullname = helpers.fullname(@user)
...
end
end
You can still use helper modules in other classes with include
.
# some/other/klass.rb
class Klass
include UserHelper
end
The old behavior was that all helpers were included in all views, and only every helper was included in the corresponding controller, for example. UserHelper
will only be included in UserController
.
To revert to this behavior, you can set config.action_controller.include_all_helpers = false
to your config/application.rb
file.
source to share