I'm learning Ruby on Rails from Michael Hartl's book. I really enjoy this writing, structured and clear. Nevertheless, I have a question about chapter 3.3 differences between Rails 3.0 and Rails 3.2 versions:
PagesController
becomes a StaticPagesController
@Title
is not anymore initialised in the controller, but in each page@Title
variable is initialised through "provide" function, and used through "yield" function instead of simply inserting <%= @title %>
Is it for training reasons, or is it an updated best practice of RoR development?
The PagesController becomes a StaticPagesController
There is no reason. PagesController is fine. He did that maybe for clarity reason (?).
@Title is not anymore initialised in the controller, but in each page
This is for best practice/convention reason. Please take a look at the answer on this question Where does the meta content live in the MVC?
The @Title variable is initialised through "provide" function, and used through "yield" function instead of simply inserting <%= @title %>
Same answer as above.
Some people think that meta content such as title belongs to the view and thus should be set in the view, not in the controller.
The practical differences between provide
and content_for
are well explained here: http://api.rubyonrails.org/classes/ActionController/Streaming.html#module-ActionController::Streaming-label-Communication+between+layout+and+template
Please note that these changes are not specific to Rails 3.2 (except for provide
that was introduce with stream
in Rails 3.1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With