From 4e533411d5df4815a78d7cd362538088931122e4 Mon Sep 17 00:00:00 2001 From: humorwang <417791635@qq.com> Date: Fri, 5 Aug 2022 10:57:13 +0800 Subject: [PATCH] first commit --- Gemfile | 2 + app/assets/config/manifest.js | 2 + .../{application.css => application.scss} | 4 +- app/controllers/articles_controller.rb | 14 +- app/controllers/welcome_controller.rb | 2 + app/helpers/flashes_helper.rb | 11 + app/javascript/application.js | 8 +- app/models/article.rb | 1 + app/models/user.rb | 4 + app/models/vote.rb | 3 + app/views/articles/_form.html.erb | 34 +- app/views/articles/edit.html.erb | 2 +- app/views/articles/index.html.erb | 14 +- app/views/articles/new.html.erb | 5 +- app/views/common/_flashes.html.erb | 8 + app/views/common/_footer.html.erb | 6 + app/views/common/_navbar.html.erb | 52 +++ app/views/layouts/application.html.erb | 12 +- config/importmap.rb | 7 + config/initializers/devise.rb | 311 ++++++++++++++++++ config/locales/devise.en.yml | 65 ++++ config/routes.rb | 11 +- db/migrate/20220805030705_create_votes.rb | 9 + .../20220805062832_add_devise_to_users.rb | 51 +++ db/schema.rb | 15 +- test/fixtures/votes.yml | 7 + test/models/vote_test.rb | 7 + 27 files changed, 633 insertions(+), 34 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (97%) create mode 100644 app/helpers/flashes_helper.rb create mode 100644 app/models/vote.rb create mode 100644 app/views/common/_flashes.html.erb create mode 100644 app/views/common/_footer.html.erb create mode 100644 app/views/common/_navbar.html.erb create mode 100644 config/initializers/devise.rb create mode 100644 config/locales/devise.en.yml create mode 100644 db/migrate/20220805030705_create_votes.rb create mode 100644 db/migrate/20220805062832_add_devise_to_users.rb create mode 100644 test/fixtures/votes.yml create mode 100644 test/models/vote_test.rb diff --git a/Gemfile b/Gemfile index b234148..7cc450b 100644 --- a/Gemfile +++ b/Gemfile @@ -52,6 +52,8 @@ gem 'jquery-rails' # gem "sassc-rails" gem 'bootstrap', '~> 5.2.0' +gem 'devise' + # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index ddd546a..04de9ad 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -2,3 +2,5 @@ //= link_directory ../stylesheets .css //= link_tree ../../javascript .js //= link_tree ../../../vendor/javascript .js +//= link bootstrap.min.js +//= link popper.js diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 97% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index 05c538d..d5741ff 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -14,4 +14,6 @@ *= require_self */ - @import "bootstrap"; \ No newline at end of file +@import 'bootstrap' + + diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 830e068..0ea1b11 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,4 +1,7 @@ class ArticlesController < ApplicationController + + before_action :authenticate_user! , only: [:new, :create] + def index @articles = Article.all end @@ -15,7 +18,7 @@ class ArticlesController < ApplicationController @article = Article.new(article_params) if @article.save - redirect_to @article + redirect_to articles_path else render :new, status: :unprocessable_entity end @@ -30,7 +33,7 @@ class ArticlesController < ApplicationController @article = Article.find(params[:id]) if @article.update(article_params) - redirect_to @article + redirect_to articles_path else render :edit, status: :unprocessable_entity end @@ -44,6 +47,13 @@ class ArticlesController < ApplicationController end + def upvote + @article = Article.find(params[:id]) + @article.votes.create + redirect_to @article + end + + private def article_params params.require(:article).permit(:title, :body) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 4f04884..40ef8ca 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,5 +1,7 @@ class WelcomeController < ApplicationController def index + # flash[:notice] = "早安!你好!我的宝贝" + flash[:alert] = "晚安!该睡了!" end diff --git a/app/helpers/flashes_helper.rb b/app/helpers/flashes_helper.rb new file mode 100644 index 0000000..455cbbd --- /dev/null +++ b/app/helpers/flashes_helper.rb @@ -0,0 +1,11 @@ +module FlashesHelper + FLASH_CLASSES = { alert: "danger", notice: "success", warning: "warning"}.freeze + + def flash_class(key) + FLASH_CLASSES.fetch key.to_sym, key + end + + def user_facing_flashes + flash.to_hash.slice "alert", "notice", "warning" + end +end diff --git a/app/javascript/application.js b/app/javascript/application.js index 0d7b494..a6ece2c 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,3 +1,7 @@ // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails -import "@hotwired/turbo-rails" -import "controllers" + +//= require popper +//= require bootstrap + + + diff --git a/app/models/article.rb b/app/models/article.rb index b72913e..29cdc2c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,4 +1,5 @@ class Article < ApplicationRecord validates :title, presence: true validates :body, presence: true, length: { minimum: 10 } + has_many :votes, class_name: "Vote", foreign_key: "article_id" end diff --git a/app/models/user.rb b/app/models/user.rb index 379658a..4756799 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,6 @@ class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable end diff --git a/app/models/vote.rb b/app/models/vote.rb new file mode 100644 index 0000000..3e06e7c --- /dev/null +++ b/app/models/vote.rb @@ -0,0 +1,3 @@ +class Vote < ApplicationRecord + belongs_to :article, class_name: "Article", foreign_key: "article_id" +end diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb index 346888c..8f8a2a2 100644 --- a/app/views/articles/_form.html.erb +++ b/app/views/articles/_form.html.erb @@ -1,21 +1,23 @@ <%= form_with model: article do |form| %> -
Find me in app/views/articles/index.html.erb
+ +Find me in app/views/articles/new.html.erb
-