I’d like to know where exactly are helpers available by default?
For example, if I have a PagesController, will my PagesHelper methods be
available at PagesController? or just at the Pages’s views?
Thank you,
Rodrigo
I’d like to know where exactly are helpers available by default?
For example, if I have a PagesController, will my PagesHelper methods be
available at PagesController? or just at the Pages’s views?
Thank you,
Rodrigo
Helpers help generate code for the view. For example, if you have an
array
of items that need to be rendered in a specific way, you can encapsulate
that functionality in a helper method, and call it from the view. It
helps
clean up the view code, and adds code reuse. So helpers are meant to be
for
views, not controllers.
On Jul 11, 8:58am, Dheeraj K. [email protected] wrote:
I’d like to know where exactly are helpers available by default?
For example, if I have a PagesController, will my PagesHelper methods be
available at PagesController? or just at the Pages’s views?
Dheeraj is correct, helpers are intended for views, but if you have a
method that you need access to in your controller and your view, and/
or your helper then, in order to keep things a little DRYer, you can
define your method in your controller, and make it available to the
view and helper as so:
class MyController < ApplicationController
helper_method :my_method
private
def my_method
# do something
end
end
Now you can access my_method from your view or your helper as well as
your controller.
hth
Paul
On 11 July 2011 08:36, Rodrigo R. [email protected] wrote:
I’d like to know where exactly are helpers available by default?
For example, if I have a PagesController, will my PagesHelper methods be
available at PagesController? or just at the Pages’s views?
In addition to other replies note that logic in controllers should
rarely be sufficiently complex so as to require helpers. Such code
should almost always be in the models.
Colin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs