About this user

Christian Romney http://www.xml-blog.com

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

DRY up empty controller actions

Ever create Rails views that don't user the controller at all just to take advantage of an application layout?
If you have, you may have a lot of code that looks like this:


class HelpController < ApplicationController
  def index
  end

  def tutorial
  end

  def faq
  end
end


Well, there's a simple way to make this a lot cleaner...


class ApplicationController < ActionController::Base
  def self.simple_action(*actions)
    actions.each {|action| class_eval("def #{action}; end")}
  end
end

Now your controller can look like this instead...

class HelpController < ApplicationController
  simple_action :index, :tutorial, :faq
end

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed