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

Logan white #290

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
54 changes: 54 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all

# run the search function if the search parameter exists
if params[:search]
@articles = Article.search(params[:search])
end
end

def show
@article = Article.find(params[:id])
end

def new
@article = Article.new
end

def create
@article = Article.new(article_params)

if @article.save
redirect_to @article
else
render :new, status: :unprocessable_entity
end
end

def edit
@article = Article.find(params[:id])
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to root_path, status: :see_other
end

private
def article_params
params.require(:article).permit(:title, :content, :author, :date)
end
end
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ArticlesHelper
end
15 changes: 15 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Article < ApplicationRecord
validates :title, presence: true
validates :content, presence: true, length: { minimum: 1 }
# No need to validate author or date (both validated by the input fields)

# function that returns all articles with title or content matching a search query
def self.search(query)
unless query.strip.empty?
where("lower(title) like :searchQuery or lower(content) like :searchQuery", searchQuery: "%#{query.downcase}%")
else
all
end
end

end
45 changes: 45 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<%= form_with model: article do |form| %>
<!-- Title -->
<div>
<%= form.label :title %><br>
<%= form.text_field :title %>
<% article.errors.full_messages_for(:title).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<!-- Author -->
<div>
<%= form.label :author %><br>
<%= form.text_field :author %><br>
<% article.errors.full_messages_for(:author).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<!-- Content -->
<div>
<%= form.label :content %><br>
<%= form.text_area :content %><br>
<% article.errors.full_messages_for(:content).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<!-- Date -->
<div>
<%= form.label :date %><br>
<%= form.date_field :date %><br>
<% article.errors.full_messages_for(:date).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<!-- Buttons -->
<div style="padding-top:10px;">
<table>
<td> <%= form.submit %> </td>
<td> <button type="button"><%= link_to "Back", :back, style: "text-decoration:none;color:#000"%></button> </td>
</table>
</div>
<% end %>
3 changes: 3 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Edit Article</h1>

<%= render "form", article: @article %>
19 changes: 19 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>Encyclopedia</h1>

<!-- Search form -->
<%= form_tag "/articles", method: "GET" do %>
<%= text_field_tag :search, params[:search] %> <%= submit_tag "Search" %> <button type="button"><%= link_to "Clear", root_path, style: "text-decoration:none;color:#000" %></button>
<% end %>

<!-- List of articles -->
<h4 style="margin-bottom:0px;">Articles</h4>
<ul style="list-style-type: none;margin-top:2px;">
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article %>
</li>
<% end %>
</ul>

<!-- New form button -->
<button type="button"> <%= link_to "Add Entry", new_article_path, style: "text-decoration:none;color:#000" %> </button>
3 changes: 3 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>New Article</h1>

<%= render "form", article: @article %>
18 changes: 18 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Display properties of article -->
<h1><%= @article.title %></h1>
<h3><%= @article.author %></h3>
<h4><%= @article.date %></h4>

<p><%= @article.content %></p>

<!-- Organized action buttons in a table for horizontal placement -->
<table>
<tr>
<td><button type="button"><%= link_to "Home", root_path, style: "text-decoration:none;color:#000" %></button></td>
<td><button type="button"><%= link_to "Edit", edit_article_path(@article), style: "text-decoration:none;color:#000" %></button></td>
<td><button type="button"><%= link_to "Destroy", article_path(@article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
}, style: "text-decoration:none;color:#000" %></button></td>
</tr>
</table>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
root "articles#index"

resources :articles
end
12 changes: 12 additions & 0 deletions db/migrate/20240129061916_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateArticles < ActiveRecord::Migration[7.1]
def change
create_table :articles do |t|
t.string :title
t.text :content
t.text :author
t.date :date

t.timestamps
end
end
end
24 changes: 24 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end