diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb
new file mode 100644
index 0000000..4554e20
--- /dev/null
+++ b/app/controllers/articles_controller.rb
@@ -0,0 +1,49 @@
+class ArticlesController < ApplicationController
+
+ def index
+ @articles = Article.alphabetical.paginate(:page => params[:page]).per_page(10)
+ end
+
+ def show
+ @article = Article.find(params[:id])
+ end
+
+
+ def new
+ @article = Article.new
+ end
+
+
+ def edit
+ @article = Article.find(params[:id])
+ end
+
+
+ def create
+ @article = Article.new(params[:article])
+ if @article.save
+ flash[:notice] = 'Article was successfully created.'
+ redirect_to article_path(@article)
+ else
+ render :action => "new"
+ end
+ end
+
+
+ def update
+ @article = Article.find(params[:id])
+ if @article.update_attributes(params[:article])
+ flash[:notice] = 'Article was successfully updated.'
+ redirect_to article_path(@article)
+ else
+ render :action => "edit"
+ end
+ end
+
+
+ def destroy
+ @article = Article.find(params[:id])
+ @article.destroy
+ redirect_to articles_path
+ end
+end
diff --git a/app/models/article.rb b/app/models/article.rb
new file mode 100644
index 0000000..4ef45fe
--- /dev/null
+++ b/app/models/article.rb
@@ -0,0 +1,10 @@
+class Article < ActiveRecord::Base
+ # relationship
+ belongs_to :category
+ # validation
+ validates_presence_of :title, :content
+
+ # scope
+ scope :alphabetical, -> {order('title')}
+ scope :active, -> {where(active: true)}
+end
diff --git a/app/models/category.rb b/app/models/category.rb
index a83f347..4e97e3a 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -1,5 +1,6 @@
class Category < ActiveRecord::Base
has_many :photos
+ has_many :articles
scope :active, where('active = ?', true)
scope :alphabetical, order('name')
diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb
new file mode 100644
index 0000000..1377666
--- /dev/null
+++ b/app/views/articles/_form.html.erb
@@ -0,0 +1,15 @@
+<%= simple_form_for @article, :html => { :class => 'form-horizontal' } do |f| %>
+
+<% end %>
diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb
new file mode 100644
index 0000000..786950e
--- /dev/null
+++ b/app/views/articles/edit.html.erb
@@ -0,0 +1 @@
+<%= render :partial => 'form' %>
diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb
new file mode 100644
index 0000000..0aaa224
--- /dev/null
+++ b/app/views/articles/index.html.erb
@@ -0,0 +1,25 @@
+Articles
+
+
+
+ Title |
+ Category |
+ Actions |
+
+
+
+ <% @articles.each do |article| %>
+
+ <%= link_to article.title, article_path(article) %> |
+ <%= article.category.name %> |
+
+ <%= link_to 'Edit', edit_article_path(article), :class => 'btn btn-mini' %>
+ <%= link_to 'Destroy', article_path(article), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
+ |
+
+ <% end %>
+
+
+<%= will_paginate @articles, :previous_label => "Previous ", :next_label => " Next" %>
+
+<%= link_to 'New Article', new_article_path, :class => 'btn btn-primary' %>
diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb
new file mode 100644
index 0000000..786950e
--- /dev/null
+++ b/app/views/articles/new.html.erb
@@ -0,0 +1 @@
+<%= render :partial => 'form' %>
diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb
new file mode 100644
index 0000000..9f07e38
--- /dev/null
+++ b/app/views/articles/show.html.erb
@@ -0,0 +1,18 @@
+<%= @article.title %>
+
+
+ <%= @article.content %>
+
+
+
+
+
+ Category:
+ <%= @article.category.name %>
+
+
+
+ <%= link_to 'Back', articles_path, :class => 'btn' %>
+ <%= link_to 'Edit', edit_article_path(@article), :class => 'btn' %>
+ <%= link_to 'Delete', article_path(@article), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
+
diff --git a/config/routes.rb b/config/routes.rb
index 3c279db..f9ba06e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -3,7 +3,7 @@
resources :categories
resources :proverbs
resources :photos
-
+ resources :articles
get 'home' => 'home#index', :as => :home
root :to => 'home#index'
diff --git a/db/development.sqlite3 b/db/development.sqlite3
index c794ffd..42cfe78 100644
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/20140422002752_create_articles.rb b/db/migrate/20140422002752_create_articles.rb
new file mode 100644
index 0000000..a12398e
--- /dev/null
+++ b/db/migrate/20140422002752_create_articles.rb
@@ -0,0 +1,12 @@
+class CreateArticles < ActiveRecord::Migration
+ def change
+ create_table :articles do |t|
+ t.string :title
+ t.text :content
+ t.integer :category_id
+ t.boolean :active
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 62bd536..d0f0a80 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,32 +9,41 @@
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
-# It's strongly recommended to check this file into your version control system.
+# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120412125204) do
+ActiveRecord::Schema.define(version: 20140422002752) do
- create_table "categories", :force => true do |t|
+ create_table "articles", force: true do |t|
+ t.string "title"
+ t.text "content"
+ t.integer "category_id"
+ t.boolean "active"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "categories", force: true do |t|
t.string "name"
- t.boolean "active", :default => true
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.boolean "active", default: true
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
- create_table "photos", :force => true do |t|
+ create_table "photos", force: true do |t|
t.string "caption"
t.integer "category_id"
- t.boolean "active", :default => true
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.boolean "active", default: true
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
t.string "image"
end
- create_table "proverbs", :force => true do |t|
+ create_table "proverbs", force: true do |t|
t.string "klingon"
t.string "translation"
- t.boolean "active", :default => true
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.boolean "active", default: true
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
end
diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml
index b67f38f..7e12f89 100644
--- a/test/fixtures/articles.yml
+++ b/test/fixtures/articles.yml
@@ -1,4 +1,4 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
diff --git a/test/models/article_test.rb b/test/models/article_test.rb
new file mode 100644
index 0000000..11c8abe
--- /dev/null
+++ b/test/models/article_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ArticleTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end