From 9b15fe6ce8ee1e67dbe379905c67006233383773 Mon Sep 17 00:00:00 2001 From: Cathy Shin Date: Mon, 21 Apr 2014 20:34:20 -0400 Subject: [PATCH 1/4] relationship and validation set --- app/models/article.rb | 10 +++++ app/models/category.rb | 1 + db/development.sqlite3 | Bin 24576 -> 26624 bytes db/migrate/20140422002752_create_articles.rb | 12 ++++++ db/schema.rb | 37 ++++++++++++------- test/fixtures/articles.yml | 2 +- test/models/article_test.rb | 7 ++++ 7 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 app/models/article.rb create mode 100644 db/migrate/20140422002752_create_articles.rb create mode 100644 test/models/article_test.rb 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/db/development.sqlite3 b/db/development.sqlite3 index c794ffdcee38c1f1659362c3e56e1c82e9d56b43..42cfe78faa89dc06f6e6780f4554d4fa0a898fd6 100644 GIT binary patch delta 287 zcmZoTz}Rqsae}lU7Xt$WFA&23`$QdMX)XrcNAkQZ_Zirk?lG|UvNN+OvYuzU$NYrl zxWpwT8JkTTliAtDWn~$gB})>Ma#9nEN-~pkQj1|UuXB*AV~DFl zh@+E_tAY|#crqWWnQ}>HNlvPgLRn%_az$wLP=^x$>c~@ z&B^mv#3%1#5#M}{)r^Ucm3QyRZNlpgcN6Ne`_Zirj?lG|UvNN+OvYuzU$NXfo zB1an2=6g&mOza=TB_$b~O((x#db;@>s~Hmy3;PZR_IK=e*iW(V*es~9f_?H1ejlLd Y76$gW?6=sDvu^>4E@9uiMcl^&0Da{n6aWAK 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 From c7d3d7f6a00c91035667b133ea05ec1e26443a27 Mon Sep 17 00:00:00 2001 From: Cathy Shin Date: Mon, 21 Apr 2014 21:03:30 -0400 Subject: [PATCH 2/4] dkj --- app/views/articles/_form.html.erb | 15 +++++++++++++++ app/views/articles/edit.html.erb | 1 + app/views/articles/index.html.erb | 25 +++++++++++++++++++++++++ app/views/articles/new.html.erb | 1 + app/views/articles/show.html.erb | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 app/views/articles/_form.html.erb create mode 100644 app/views/articles/edit.html.erb create mode 100644 app/views/articles/index.html.erb create mode 100644 app/views/articles/new.html.erb create mode 100644 app/views/articles/show.html.erb 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| %> +
+ <%= controller.action_name.capitalize %> Article + + <%= f.input :title %> + <%= f.input :content %> + <%= f.input :category_id, :collection => Category.active.alphabetical, :include_blank => true %> + <%= f.input :active %> + +
+ <%= f.submit nil, :class => 'btn btn-primary' %> + <%= link_to 'Cancel', articles_path, :class => 'btn' %> +
+
+<% 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

+ + + + + + + + + + <% @articles.each do |article| %> + + + + + + <% end %> + +
TitleCategoryActions
<%= 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' %> +
+<%= 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' %> +
From cc62f7f22fd88527f9b0778ac00df81795061ca8 Mon Sep 17 00:00:00 2001 From: Cathy Shin Date: Mon, 21 Apr 2014 21:15:29 -0400 Subject: [PATCH 3/4] Added routes! --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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' From 4f96979e21ad3b5f7eba810f7a64dcae8eab4b5a Mon Sep 17 00:00:00 2001 From: Cathy Shin Date: Mon, 21 Apr 2014 21:16:34 -0400 Subject: [PATCH 4/4] Fake commit to get it working --- app/controllers/articles_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index da1559f..4554e20 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -4,7 +4,6 @@ def index @articles = Article.alphabetical.paginate(:page => params[:page]).per_page(10) end - def show @article = Article.find(params[:id]) end