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 @@
Название | +Автор | + ++ | ||
---|---|---|---|---|
<%= article.title %> | +<%= article.author %> | + +<%= 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' %> | +
<%= @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