Understanding where certain 'inherited' methods come from in ActionController::Base

2 views
Skip to first unread message

Manuca

unread,
Dec 16, 2009, 9:06:39 PM12/16/09
to Ruby on Rails: Talk
Hi I just started getting my hands on Rails, I've been reading a good
book on the subject but now I want to continue by myself and with the
help of the rails api documentation but I find it quite difficult to
follow (copared with the Java generated docs for example).

One thing that I still can't get to understand is how methods like
'layout' become available on my controllers to mention one example I
supposed that the method was defined inside ActionController::Base but
reading the docs I see there is no such thing defined there.

Can you please clarify on this issue, I believe it may be something
related to the Ruby that I still don't know about.

Thanks in advance.
M.

Leonardo Mateo

unread,
Dec 17, 2009, 7:31:10 AM12/17/09
to rubyonra...@googlegroups.com
Try apidock.com.

http://apidock.com/rails/ActionController

Hope it helps.

--
Leonardo Mateo.
There's no place like ~

Manuca

unread,
Dec 17, 2009, 2:59:25 PM12/17/09
to Ruby on Rails: Talk
On Dec 17, 9:31 am, Leonardo Mateo <leonardoma...@gmail.com> wrote:
> Try apidock.com.
> http://apidock.com/rails/ActionController
> Hope it helps.
>

Ok thanks for the info, those docs seem more friendly, anyway, I still
don't understand where those methods (for ex. layout) get inherited or
injected don't know how that magic happens they aren't mentioned even
in apidock.com.

Thanks
m.

Hassan Schroeder

unread,
Dec 17, 2009, 3:13:30 PM12/17/09
to rubyonra...@googlegroups.com
On Thu, Dec 17, 2009 at 11:59 AM, Manuca <man...@gmail.com> wrote:

> Ok thanks for the info, those docs seem more friendly, anyway, I still
> don't understand where those methods (for ex. layout) get inherited or
> injected don't know how that magic happens

when in doubt, cd to one of your projects and e.g.
find vendor/rails -type f -exec grep -H 'def layout' {} \;

You'll see exactly how the magic happens :-) (and in this case, also
lots o' comments).

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan

Manuca

unread,
Dec 17, 2009, 3:48:25 PM12/17/09
to Ruby on Rails: Talk
On Dec 17, 5:13 pm, Hassan Schroeder <hassan.schroe...@gmail.com>
wrote:

> when in doubt, cd to one of your projects and e.g.
>   find vendor/rails  -type f -exec grep -H 'def layout' {} \;

Thanks again for the feedback, yep I already did a recursive grep
inside my gems dirs and found those definitions I guess there i'll
find the real info, anyway what I find confusing is that in the docs
those automatic inclusions are not mentioned, maybe those are obvious
or implicit and they can be derived from the module name I really
don't know because Im also new to the Ruby language.

Is it possible that because both ActionController::Base and
ActionController::Layout::ClassMethods share th 'ActionController'
module they are inside the same namespace and all public methods of
Layout are available to Base? Can I conclude that this is the case
with all clases inside ActionController?

Please excuse my enormouse ignorance on the subject
Thanks for you patience!
m.

Yehuda Katz

unread,
Dec 17, 2009, 4:21:21 PM12/17/09
to Ruby on Rails: Talk
It might be easier to understand what's going on in Rails master. Take
a look at http://github.com/rails/rails/blob/master/actionpack/lib/action_controller/base.rb#L5-37.
As you can see, the Base class is including a series of modules. In
Ruby, modules are like dynamic superclasses; including one adds the
module to the superclass chain.

For instance, observe that ActionController::Base includes
ActionController::UrlFor. You can follow that module to
http://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/url_for.rb,
which has the much-used url_for method (http://github.com/rails/rails/
blob/master/actionpack/lib/action_controller/metal/url_for.rb#L28-39).

Hope this helps.

-- Yehuda

Manuca

unread,
Dec 18, 2009, 9:52:48 PM12/18/09
to Ruby on Rails: Talk
On Dec 17, 6:21 pm, Yehuda Katz <wyc...@gmail.com> wrote:
> Hope this helps.

Thanks Yehuda that was of help.
m.

Reply all
Reply to author
Forward
0 new messages