diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index ddd546a0b..4028c22a6 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -1,4 +1,4 @@ //= link_tree ../images -//= link_directory ../stylesheets .css //= link_tree ../../javascript .js //= link_tree ../../../vendor/javascript .js +//= link_tree ../builds diff --git a/app/assets/stylesheets/application.bootstrap.scss b/app/assets/stylesheets/application.bootstrap.scss new file mode 100644 index 000000000..e8d5a1c9e --- /dev/null +++ b/app/assets/stylesheets/application.bootstrap.scss @@ -0,0 +1,2 @@ +@import 'bootstrap/scss/bootstrap'; +@import 'bootstrap-icons/font/bootstrap-icons'; \ No newline at end of file diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 000000000..852275863 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,69 @@ +class ArticlesController < ApplicationController + # 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 + 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 + 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 } + end + end + + # Destroys the specified article instance, rendering turbo_stream + def destroy + @article.destroy! + + respond_to do |format| + format.turbo_stream + end + 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 +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 000000000..13fac9077 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +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 diff --git a/app/javascript/application.js b/app/javascript/application.js index 0d7b49404..d16fcbe2f 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,3 +1,4 @@ // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails import "@hotwired/turbo-rails" import "controllers" +import * as bootstrap from "bootstrap" diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 000000000..53a93dfa7 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +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 diff --git a/app/views/articles/_article.html.erb b/app/views/articles/_article.html.erb new file mode 100644 index 000000000..320abd1f4 --- /dev/null +++ b/app/views/articles/_article.html.erb @@ -0,0 +1,28 @@ +<%= turbo_frame_tag article do %> +
+ Title: + <%= link_to article.title, article, data: { turbo_frame: '_top'} %> +
+ ++ Content: + <%= article.content %> +
+ ++ Author: + <%= article.author %> +
+ ++ Date: + <%= article.date %> +
+ +CRUD with Rails
+