Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local testing #309

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -248,6 +253,7 @@ GEM
PLATFORMS
aarch64-linux
ruby
x64-mingw32
x86_64-linux

DEPENDENCIES
Expand Down
58 changes: 58 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
@@ -0,0 +1,2 @@
module ArticlesHelper
end
10 changes: 10 additions & 0 deletions 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
36 changes: 36 additions & 0 deletions app/views/articles/_form.html.erb
@@ -0,0 +1,36 @@
<%= form_with(model: @article, local: true) do |form| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, 'error') %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>

<div class="field">
<%= form.label :author %>
<%= form.text_field :author %>
</div>

<div class="field">
<%= form.label :date %>
<%= form.date_field :date %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions app/views/articles/edit.html.erb
@@ -0,0 +1,4 @@
<h1>Edit Article</h1>
<%= render 'form' %>
<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>
6 changes: 6 additions & 0 deletions app/views/articles/index.html.erb
@@ -0,0 +1,6 @@
<h1>Articles</h1>
<ul>
<% @articles.each do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
3 changes: 3 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,3 @@
<h1>New Article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
6 changes: 6 additions & 0 deletions app/views/articles/show.html.erb
@@ -0,0 +1,6 @@
<h1><%= @article.title %></h1>
<p><%= @article.content %></p>
<p><strong>Author:</strong> <%= @article.author %></p>
<p><strong>Date:</strong> <%= @article.date %></p>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
11 changes: 4 additions & 7 deletions 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
12 changes: 12 additions & 0 deletions 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
23 changes: 23 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions 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
13 changes: 13 additions & 0 deletions 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