From 9287ad8a8aed55b19e8855fab4700786cc0bd8e7 Mon Sep 17 00:00:00 2001 From: Tawana Mphusu Date: Mon, 29 Jan 2024 23:05:24 -0500 Subject: [PATCH 1/4] generated the article model and migration --- Gemfile.lock | 6 ++++++ app/controllers/articles_controller.rb | 4 ++++ app/helpers/articles_helper.rb | 2 ++ app/models/article.rb | 2 ++ app/views/articles/index.html.erb | 2 ++ config/routes.rb | 10 ++-------- db/migrate/20240130040404_create_articles.rb | 12 ++++++++++++ test/controllers/articles_controller_test.rb | 7 +++++++ test/fixtures/articles.yml | 13 +++++++++++++ 9 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 app/models/article.rb create mode 100644 app/views/articles/index.html.erb create mode 100644 db/migrate/20240130040404_create_articles.rb create mode 100644 test/controllers/articles_controller_test.rb create mode 100644 test/fixtures/articles.yml diff --git a/Gemfile.lock b/Gemfile.lock index 2b317df1b..0e57e6756 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -147,6 +147,8 @@ GEM racc (~> 1.4) nokogiri (1.15.5-aarch64-linux) racc (~> 1.4) + nokogiri (1.15.5-x64-mingw32) + racc (~> 1.4) nokogiri (1.15.5-x86_64-linux) racc (~> 1.4) psych (5.1.1.1) @@ -215,6 +217,7 @@ GEM sqlite3 (1.6.9) mini_portile2 (~> 2.8.0) sqlite3 (1.6.9-aarch64-linux) + sqlite3 (1.6.9-x64-mingw32) sqlite3 (1.6.9-x86_64-linux) stimulus-rails (1.3.0) railties (>= 6.0.0) @@ -227,6 +230,8 @@ GEM railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + tzinfo-data (1.2023.4) + tzinfo (>= 1.0.0) web-console (4.2.1) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -248,6 +253,7 @@ GEM PLATFORMS aarch64-linux ruby + x64-mingw32 x86_64-linux DEPENDENCIES diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 000000000..e5904fd2e --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,4 @@ +class ArticlesController < ApplicationController + def index + end +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 000000000..296827759 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 000000000..b7a72b589 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,2 @@ +class Article < ApplicationRecord +end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 000000000..ab9c55261 --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,2 @@ +

Articles#index

+

Find me in app/views/articles/index.html.erb

diff --git a/config/routes.rb b/config/routes.rb index a125ef085..41856b32d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,4 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. - # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check - - # Defines the root path route ("/") - # root "posts#index" + root "articles#index" + get "/articles", to: "articles#index" end diff --git a/db/migrate/20240130040404_create_articles.rb b/db/migrate/20240130040404_create_articles.rb new file mode 100644 index 000000000..84add4e46 --- /dev/null +++ b/db/migrate/20240130040404_create_articles.rb @@ -0,0 +1,12 @@ +class CreateArticles < ActiveRecord::Migration[7.1] + def change + create_table :articles do |t| + t.string :title + t.text :content + t.string :author + t.date :date + + t.timestamps + end + end +end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 000000000..0a82cbb19 --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 000000000..a3f27c506 --- /dev/null +++ b/test/fixtures/articles.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyText + author: MyString + date: 2024-01-29 + +two: + title: MyString + content: MyText + author: MyString + date: 2024-01-29 From 9c598861288db7d131258ffc2d8a04a4448735dd Mon Sep 17 00:00:00 2001 From: Tawana Mphusu Date: Mon, 29 Jan 2024 23:09:13 -0500 Subject: [PATCH 2/4] implemented CRUD actions and search function --- app/controllers/articles_controller.rb | 46 ++++++++++++++++++++++++++ app/models/article.rb | 6 ++++ config/routes.rb | 4 +-- db/schema.rb | 23 +++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 db/schema.rb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e5904fd2e..d3814e759 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,4 +1,50 @@ class ArticlesController < ApplicationController + before_action :set_article, only: [:show, :edit, :update, :destroy] + def index + @articles = Article.all + end + + def show + end + + def new + @article = Article.new + end + + def create + @article = Article.new(article_params) + + if @article.save + redirect_to @article, notice: 'Article was successfully created.' + else + render :new + end + end + + def edit + end + + def update + if @article.update(article_params) + redirect_to @article, notice: 'Article was successfully updated.' + else + render :edit + end + end + + def destroy + @article.destroy + redirect_to articles_url, notice: 'Article was successfully destroyed.' + end + + private + + def set_article + @article = Article.find(params[:id]) + end + + def article_params + params.require(:article).permit(:title, :content, :author, :date) end end diff --git a/app/models/article.rb b/app/models/article.rb index b7a72b589..a65dc05c3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,2 +1,8 @@ class Article < ApplicationRecord + validates :title, presence: true + validates :content, presence: true + + def self.search(query) + where('title LIKE ? OR content LIKE ?', "%#{query}%", "%#{query}%") + end end diff --git a/config/routes.rb b/config/routes.rb index 41856b32d..6b714d7f0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ Rails.application.routes.draw do - root "articles#index" - get "/articles", to: "articles#index" + resources :articles + root 'articles#index' end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..e69eed1e7 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2024_01_30_040404) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.text "content" + t.string "author" + t.date "date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From 27be897da16d3b2fdf6d2c8094298c111bfc5d77 Mon Sep 17 00:00:00 2001 From: Tawana Mphusu Date: Mon, 29 Jan 2024 23:13:23 -0500 Subject: [PATCH 3/4] addition of other html pages to show CRUD actions working --- app/views/articles/_form.html.erb | 36 +++++++++++++++++++++++++++++++ app/views/articles/edit.html.erb | 4 ++++ app/views/articles/index.html.erb | 8 +++++-- app/views/articles/new.html.erb | 3 +++ app/views/articles/show.html.erb | 6 ++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 app/views/articles/_form.html.erb create mode 100644 app/views/articles/edit.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 000000000..8c8babe18 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,36 @@ +<%= form_with(model: @article, local: true) do |form| %> + <% if @article.errors.any? %> +
+

<%= pluralize(@article.errors.count, 'error') %> prohibited this article from being saved:

+
    + <% @article.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :title %> + <%= form.text_field :title %> +
+ +
+ <%= form.label :content %> + <%= form.text_area :content %> +
+ +
+ <%= form.label :author %> + <%= form.text_field :author %> +
+ +
+ <%= form.label :date %> + <%= form.date_field :date %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 000000000..265bbddcf --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,4 @@ +

Edit Article

+<%= render 'form' %> +<%= link_to 'Show', @article %> | +<%= link_to 'Back', articles_path %> \ No newline at end of file diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index ab9c55261..68e7a70eb 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,2 +1,6 @@ -

Articles#index

-

Find me in app/views/articles/index.html.erb

+

Articles

+
    + <% @articles.each do |article| %> +
  • <%= link_to article.title, article %>
  • + <% end %> +
diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 000000000..098ab3bf2 --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,3 @@ +

New Article

+<%= render 'form' %> +<%= link_to 'Back', articles_path %> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 000000000..81cbe751f --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,6 @@ +

<%= @article.title %>

+

<%= @article.content %>

+

Author: <%= @article.author %>

+

Date: <%= @article.date %>

+<%= link_to 'Edit', edit_article_path(@article) %> | +<%= link_to 'Back', articles_path %> \ No newline at end of file From 97e213970ff0f1826c8950a9b48192aa4e1ce0e4 Mon Sep 17 00:00:00 2001 From: Tawana Mphusu Date: Mon, 29 Jan 2024 23:44:25 -0500 Subject: [PATCH 4/4] addition of comments --- app/controllers/articles_controller.rb | 8 ++++++++ app/models/article.rb | 2 ++ config/routes.rb | 3 +++ 3 files changed, 13 insertions(+) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index d3814e759..5a8294f10 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,10 +1,13 @@ class ArticlesController < ApplicationController + #before actions for the CRUD actions before_action :set_article, only: [:show, :edit, :update, :destroy] + #list all articles def index @articles = Article.all end + def show end @@ -12,6 +15,7 @@ def new @article = Article.new end + # create action to handle form submission and creata a new article def create @article = Article.new(article_params) @@ -25,6 +29,7 @@ def create def edit end + #update action to for form submission and updating exisiting articles def update if @article.update(article_params) redirect_to @article, notice: 'Article was successfully updated.' @@ -33,6 +38,7 @@ def update end end + #Destroy action to delete exisiting articles def destroy @article.destroy redirect_to articles_url, notice: 'Article was successfully destroyed.' @@ -40,10 +46,12 @@ def destroy private + #before action based on if parameter def set_article @article = Article.find(params[:id]) end + #strong parameters to whitelist input for create and update actions def article_params params.require(:article).permit(:title, :content, :author, :date) end diff --git a/app/models/article.rb b/app/models/article.rb index a65dc05c3..95dbbee67 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,7 +1,9 @@ class Article < ApplicationRecord + #validations to ensure that the fields are present validates :title, presence: true validates :content, presence: true + #method for searching articles based on a query def self.search(query) where('title LIKE ? OR content LIKE ?', "%#{query}%", "%#{query}%") end diff --git a/config/routes.rb b/config/routes.rb index 6b714d7f0..207173a82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + #defined RESTful routes for articles resources :articles + + #set the root route to articles#index root 'articles#index' end