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
-