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..5a8294f10 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,58 @@ +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 + + def new + @article = Article.new + end + + # create action to handle form submission and creata a new article + 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 + + #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.' + else + render :edit + end + end + + #Destroy action to delete exisiting articles + def destroy + @article.destroy + redirect_to articles_url, notice: 'Article was successfully destroyed.' + end + + 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 +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..95dbbee67 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,10 @@ +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 +end 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? %> +
<%= @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 diff --git a/config/routes.rb b/config/routes.rb index a125ef085..207173a82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,7 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + #defined RESTful routes for articles + resources :articles - # 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" + #set the root route to articles#index + root '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/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 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