2. ActionPack
2.1 Trim out newlines in ERB templates
You now have the option of making ERB templates swallow up the newlines by using < if someting -> instead of just < if something >.
For example:
class SomeController < AbstractApplicationController
<% if options[:scaffold] %>
scaffold :<%= singular_name %>
<% end %>
helper :post
...produces this on post as singular_name:
class SomeController < AbstractApplicationController
scaffold :post
helper :post
...where as:
class SomeController < AbstractApplicationController
<% if options[:scaffold] -%>
scaffold :<%= singular_name %>
<% end -%>
helper :post
...produces:
class SomeController < AbstractApplicationController
scaffold :post
helper :post
2.2 New interface to cookies
There is now a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated.
Examples for writing:
cookies["user_name"] = "david" # => Will set a simple session cookie
cookies["login"] = { "value" => "XJ-122", "expires" => Time.now + 360} # => Will set a cookie that expires in 1 hour
Examples for reading:
cookies["user_name"] # => "david"
cookies.size # => 2
NOTE: If you were using the old accessor (cookies instead of @cookies),
this could potentially break your code—if you expect a full cookie
object!
2.3 Define method_missing for controllers
You can now define method_missing on a controller which will handle all requests for actions not otherwise defined.
2.4 Controllers automatically require their own helpers
Controllers will automatically require their own helper if possible. So instead of doing:
Before:
class MsgController < AbstractApplicationController
helper :msg
end
After:
class MsgController < AbstractApplicationController
end
2.5 New dependency model on controllers
class MsgController < AbstractApplicationController
model :post, :comment, :attachment
service :notification_service
observer :comment_observer
end
These new “keywords” remove the need for explicitly calling ‘require’
in most cases. The observer method even instantiates the observer as
well as requiring it.
2.6 render_partials has a default counter now too
render_partial will always by default include a counter with value 1 unless there is a counter passed in via the local_assigns hash that overrides it. As a result, render_collection_of_partials can still be written in terms of render_partial and partials that make use of a counter can be called without problems from both render_collection_of_partials as well as render_partial
2.7 New require_dependency method
require 'login_system'
becomes
require_dependency 'login_system'
You should replace every occurrence of require in your project with require_dependency otherwise
you can hit some strange behavior.