Forked from Odaeus/application_controller.rb
Created
April 23, 2013 20:43
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Articles | |
-# Simply call the articles method instead of the instance variable. | |
- articles.each do |article| | |
%article | |
%h1.title= article.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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