Thursday, August 16, 2012

Share methods between controller and views in Rails

The key is to put methods into ApplicationHelper and call them in the controller

module ApplicationHelper
  def user_unauthorized?
    ...
  end
end

class HomeController < ApplicationController
  def index
    redirect_to '/foo' if view_context.user_unauthorized?
  end
end

No comments: