Skip to content

Instantly share code, notes, and snippets.

@arekt
Forked from Odaeus/application_controller.rb
Created April 23, 2013 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arekt/5447209 to your computer and use it in GitHub Desktop.
Save arekt/5447209 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
# Creates an accessor which is exposed to the view
def self.view_accessor(*names)
attr_accessor *names
helper_method *names
end
end
class ArticlesController < ApplicationController
# List the variables/methods you want to expose to the view
view_accessor :article, :articles
def index
# Assign the variable in the action
self.articles = Article.all
end
def show
end
protected
# Or implement directly as a method
def article
Article.find(params[:id])
end
end
%h1 Articles
-# Simply call the articles method instead of the instance variable.
- articles.each do |article|
%article
%h1.title= article.title
require 'spec_helper'
# Easy to test as well.
describe ArticlesController do
describe "index" do
let!(:articles) { create_list :article, 3 }
it "assigns all the articles" do
get :index
controller.articles.should == articles
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment