From dccabf58fa89683645c12a7790a7206d8c0a05bb Mon Sep 17 00:00:00 2001 From: Dilray <115149242+Dilray@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:55:48 +0300 Subject: [PATCH] Big update Added new buttons such as: - Edit the article - Delete the article - Create an article - View the article Added a new design of the main page Added the design of the page for Adding a new article Added work with the database. all Articles are now edited in the user Database at any operation with them --- app/controllers/articles_controller.rb | 52 ++++---- app/views/articles/edit.html.erb | 14 ++- app/views/articles/index.html.erb | 96 +++++++++++---- app/views/articles/new.html.erb | 122 +++++++++++++++---- app/views/articles/show.html.erb | 2 +- config/routes.rb | 20 +-- db/migrate/20241119181830_create_articles.rb | 13 ++ db/schema.rb | 11 +- 8 files changed, 233 insertions(+), 97 deletions(-) create mode 100644 db/migrate/20241119181830_create_articles.rb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 85937bd..e03130c 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,49 +1,47 @@ class ArticlesController < ApplicationController before_action :set_article, only: [:show, :edit, :update, :destroy] - - def index - @articles = Article.all - end - + def index + @articles = Article.all + end def show end - def new @article = Article.new end - def create - @article = Article.new(article_params) - if @article.save - redirect_to @article, notice: 'Article was successfully created.' - else - render :new + def edit end - end - - def edit - end - def update - if @article.update(article_params) - redirect_to @article, notice: 'Article was successfully updated.' - else - render :edit + def update + if @article.update(article_params) + redirect_to @article, notice: 'Article was successfully updated.' + else + render :edit + end end - end def destroy + @article = Article.find(params[:id]) # Ищем статью по ID @article.destroy redirect_to articles_url, notice: 'Article was successfully destroyed.' end - private + def set_article + @article = Article.find(params[:id]) + end - def set_article - @article = Article.find(params[:id]) + def create + @article = Article.new(article_params) + if @article.save + redirect_to articles_path, notice: 'Article was successfully created.' + else + render :new + end end + private + def article_params - params.require(:article).permit(:title, :body) + params.require(:article).permit(:title, :author, :content) end -end +end \ No newline at end of file diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb index d74eece..41e21b8 100644 --- a/app/views/articles/edit.html.erb +++ b/app/views/articles/edit.html.erb @@ -19,14 +19,18 @@
- <%= form.label :body %> - <%= form.text_area :body %> + <%= form.label :author %> + <%= form.text_field :author %> +
+ +
+ <%= form.label :content %> + <%= form.text_area :content %>
- <%= form.submit %> + <%= form.submit "Update Article" %>
<% end %> -<%= link_to 'Show', @article %> | -<%= link_to 'Back', '/' %> +<%= link_to 'Back', articles_path %> diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 3e557a3..604777c 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -91,34 +91,65 @@ justify-content: center; }*/ - .destroy-button { - background-color: red; - color: white; + .btn { + padding: 10px 15px; border: none; border-radius: 5px; - padding: 5px; cursor: pointer; + font-size: 16px; + transition: background-color 0.3s, transform 0.2s; } - .destroy-button:hover { - background-color: darkred; + .btn-show { + background-color: #8b4513; /* Коричневый */ + color: white; } - /* Кнопка добавления статей */ - .add-button { + .btn-edit { + background-color: #8b4513; /* Коричневый */ color: white; - background-color: red; - padding: 10px 15px; - border: none; - border-radius: 5px; - cursor: pointer; - font-family: Arial, sans-serif; - font-weight: bold; - transition: background-color 0.3s; } - .add-button:hover { - background-color: darkred; + .btn-delete { + background-color: #f8173e; /* Красный */ + color: white; + } + + .btn:hover { + transform: scale(1.05); + } + + .btn-show:hover { + background-color: #45a049; /* Темно-зеленый при наведении */ + } + + .btn-edit:hover { + background-color: #1e88e5; /* Темно-синий при наведении */ + } + + .btn-delete:hover { + background-color: #d32f2f; /* Темно-красный при наведении */ + } + + td { + padding: 10px; /* Отступы внутри ячеек */ + text-align: left; /* Выравнивание текста по левому краю */ + } + + .article-title { + font-weight: bold; /* Жирный шрифт для заголовка статьи */ + font-size: 18px; /* Размер шрифта для заголовка */ + color: #333; /* Цвет текста */ + } + + .article-author { + font-style: italic; /* Курсив для имени автора */ + color: #555; /* Цвет текста для автора */ + } + + /* Добавление эффекта при наведении на строку таблицы */ + tr:hover td { + background-color: #f0f0f0; /* Цвет фона при наведении */ } @@ -169,7 +200,28 @@ --> - -
- -
\ No newline at end of file +<%= button_to 'Новая статья', new_article_path, method: :get, class: 'btn btn-primary' %> + + + + + + + + + + + + + <% @articles.each do |article| %> + + + + + + + + + <% end %> + +
НазваниеАвтор
<%= article.title %><%= button_to 'Показать', article_path(article), method: :get, class: 'btn btn-show' %><%= button_to 'Изменить', edit_article_path(article), method: :get, class: 'btn btn-edit' %><%= button_to 'Удалить', article_path(article), method: :delete, data: { confirm: 'Вы уверены?' }, class: 'btn btn-delete' %>
\ No newline at end of file diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb index 3ff4783..cd77a61 100644 --- a/app/views/articles/new.html.erb +++ b/app/views/articles/new.html.erb @@ -1,42 +1,110 @@ -

New Article

+ + +

Новая статья

+ +<%= form_with(model: @article, local: true, html: { class: 'article-form' }) do |form| %> <% if @article.errors.any? %> -
+

<%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:

-
-
    - <% @article.errors.full_messages.each do |message| %> -
  • <%= message %>
  • - <% end %> -
-
+
<% end %>
-
- <%= form.label :title %> -
-
- <%= form.text_field :title %> -
+ <%= form.label "Название", class: 'label' %> + <%= form.text_field :title, class: 'input' %>
-
- <%= form.label :body %> -
-
- <%= form.text_area :body %> -
+ <%= form.label "Автор", class: 'label' %> + <%= form.text_field :author, class: 'input' %> +
+ +
+ <%= form.label "Текст вашей статьи", class: 'label' %> + <%= form.text_area :content, class: 'textarea' %>
- <%= form.submit %> + <%= form.submit 'Опубликовать статью', class: 'submit-button' %>
<% end %> - - diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 05bb2ee..48ae39d 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -1,6 +1,6 @@

<%= @article.title %>

-

<%= @article.body %>

+

<%= @article.content %>

<%= link_to 'Edit', edit_article_path(@article) %> | <%= link_to 'Back', '/' %> diff --git a/config/routes.rb b/config/routes.rb index ec18219..084be88 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,19 +1,12 @@ Rails.application.routes.draw do - #root 'pages#home' - #devise_for :users, controllers: { - # sessions: 'users/sessions', - # registrations: 'users/registrations', - #omniauth_callbacks: 'users/omniauth_callbacks' - #} resources :articles root "articles#index" - get 'articles/index' => 'articles#index' - get 'articles/show' => 'articles#show' - get 'articles/new' => 'articles#new' - get 'articles/create' => 'articles#create' - get 'articles/edit' => 'articles#edit' - get 'articles/update' => 'articles#update' - get 'articles/destroy' => 'articles#destroy' + # get 'articles/index' => 'articles#index' + # get 'articles/show' => 'articles#show' + # get 'articles/new' => 'articles#new' + # get 'articles/create' => 'articles#create' + # get 'articles/edit' => 'articles#edit' + # get 'articles/update' => 'articles#update' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") @@ -27,5 +20,4 @@ sessions: 'users/sessions', registrations: 'users/registrations' } - end diff --git a/db/migrate/20241119181830_create_articles.rb b/db/migrate/20241119181830_create_articles.rb new file mode 100644 index 0000000..3450f34 --- /dev/null +++ b/db/migrate/20241119181830_create_articles.rb @@ -0,0 +1,13 @@ +class CreateArticles < ActiveRecord::Migration[8.0] + def change + create_table :articles do |t| + t.string :title, null: false + t.string :author, null: false + t.text :content, null: false + t.integer :rating, null: false, default: 0 + # t.datetime :created_at, default: -> { 'CURRENT_TIMESTAMP' } + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 74a6b8d..2216d42 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_11_16_151254) do +ActiveRecord::Schema[8.0].define(version: 2024_11_19_181830) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" + create_table "articles", force: :cascade do |t| + t.string "title", null: false + t.string "author", null: false + t.text "content", null: false + t.integer "rating", default: 0, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", id: false, force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false