a simple man :: on a mission
Leveraging the power of inheritance in Rails.
Here’s a neat trick. It’s pretty simple but really helpful.
Problem: You’re always setting up the same stuff in every controller. Layouts, helper methods and so on.
Solution: Set up your own controller to inherit from.
Throw this into a new file in the controllers folder called base_controller.rb.
class BaseController < ApplicationController
layout "base"
helper :my_cool_helper
end
layout "base"
helper :my_cool_helper
end
Now take any controller and change the first line like below. Now anything in BaseController you get for free. So in the example below, PostsController’s layout will be “base” and will have the MyCoolHelper included also.
class PostsController < BaseController
def list
end
end
def list
end
end
Wow, that was easy. Now it’s much easier to make (and change) default behaviors in your app. Hope this helps you, it sure helped me.
| Print article | This entry was posted by jondruse on June 22, 2009 at 9:52 pm, and is filed under Rails, Ruby. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |