Skip to content

Commit

Permalink
Add comments & validations
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshalc committed Jan 30, 2024
1 parent b470e32 commit e344c0e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 26 deletions.
22 changes: 18 additions & 4 deletions app/controllers/articles_controller.rb
@@ -1,41 +1,53 @@
class ArticlesController < ApplicationController
before_action :set_article, only: %i[ show edit update destroy ]
# Ensures that the set_article method is called before the specified actions
before_action :set_article, only: %i[show edit update destroy]

# Displays a list of articles, filtered by title if a query parameter is present
def index
@articles = Article.all
if params[:query].present?
@articles = Article.where("title LIKE ?", params[:query])
else
@articles = Article.all
end
end

# Displays the details of a specific article
def show
end

# Initializes a new article instance for creation
def new
@article = Article.new
end

# Renders the form for editing an existing article
def edit
end

# Creates a new article based on parameters, and renders turbo_stream if successful
def create
@article = Article.new(article_params)
if @article.save
respond_to do |format|
format.turbo_stream
end
else
format.html { render :new, status: :unprocessable_entity }
render :new, status: :unprocessable_entity
end
end

# Updates an existing article with the provided parameters, redirects on success
def update
if @article.update(article_params)
respond_to do |format|
format.html { redirect_to article_url(@article), notice: "Article was successfully updated." }
end
else
format.html { render :edit, status: :unprocessable_entity }
format.html { render :edit, status: :unprocessable_entity }
end
end

# Destroys the specified article instance, rendering turbo_stream
def destroy
@article.destroy!

Expand All @@ -45,10 +57,12 @@ def destroy
end

private
# Sets the @article instance variable based on the provided ID parameter
def set_article
@article = Article.find(params[:id])
end

# Defines the permitted parameters for article creation and updates
def article_params
params.require(:article).permit(:title, :content, :author, :date)
end
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/articles_helper.rb
@@ -1,2 +1,9 @@
module ArticlesHelper
def display_errors_for_field(object, field)
if object.errors.any?
object.errors.full_messages_for(field).each do |message|
concat content_tag(:div, message, class: 'text-danger')
end
end
end
end
6 changes: 5 additions & 1 deletion app/models/article.rb
@@ -1,6 +1,10 @@
class Article < ApplicationRecord

# Validates presence of title and content fields
validates :title, presence: true
validates :content, presence: true

# Defines a class method to search for articles based on a query
def self.search(query)
where("title LIKE :query OR content LIKE :query", query: "%#{query}%")
end
end
2 changes: 1 addition & 1 deletion app/views/articles/_article.html.erb
Expand Up @@ -2,7 +2,7 @@
<div class="card p-4 mt-2">
<p>
<strong>Title:</strong>
<%= article.title %>
<%= link_to article.title, article, data: { turbo_frame: '_top'} %>
</p>

<p>
Expand Down
16 changes: 4 additions & 12 deletions app/views/articles/_form.html.erb
@@ -1,25 +1,16 @@
<div class="card p-4 mt-1">
<%= form_with(model: article) do |form| %>
<% if article.errors.any? %>
<div style="color: red">
<h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>

<ul>
<% article.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title, class: 'form-control' %>
<% display_errors_for_field(@article, :title) %>
</div>

<div>
<%= form.label :content, style: "display: block" %>
<%= form.text_field :content, class: 'form-control' %>
<% display_errors_for_field(@article, :content) %>
</div>

<div>
Expand All @@ -31,8 +22,9 @@
<%= form.hidden_field :date, class: 'form-control', value: Time.now %>
</div>

<div>
<div class="d-flex gap-2">
<%= form.submit class: 'btn btn-success mt-2 w-100' %>
<%= link_to 'Cancel', root_path, class: 'btn btn-danger mt-2'%>
</div>
<% end %>
</div>
14 changes: 14 additions & 0 deletions app/views/articles/index.html.erb
@@ -1,8 +1,22 @@
<h1 class="text-dark fw-bold">Encyclopedia</h1>
<p class="text-muted">CRUD with Rails</p>
<hr>

<%= form_with(url: articles_path, method: :get, data: { turbo_frame: "article", turbo_action: 'advance' }) do |form| %>
<div class="d-flex gap-1">
<%= form.text_field :query, class: 'form-control' %>
<%= form.submit class: 'btn btn-success' %>
<% if params[:query].present? %>
<%= link_to 'Clear', articles_path, class: 'btn btn-danger' %>
<% end %>
</div>
<% end %>

<hr>

<%= link_to 'Add Article', new_article_path, data: { turbo_frame: dom_id(Article.new) }, class: 'btn btn-dark' %>
<%= turbo_frame_tag Article.new %>

<div id="articles" class="d-flex gap-3">
<% @articles.each do |article| %>
<%= render article %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
root "articles#index"
get '/search', to: "articles#search"
resources :articles
get "up" => "rails/health#show", as: :rails_health_check
end
16 changes: 8 additions & 8 deletions test/fixtures/articles.yml
@@ -1,11 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
content: MyString
author: MyString
# one:
# title: MyString
# content: MyString
# author: MyString

two:
title: MyString
content: MyString
author: MyString
# two:
# title: MyString
# content: MyString
# author: MyString

0 comments on commit e344c0e

Please sign in to comment.