From c24c1e0628ce112036b66b3842ff95aeb2633d12 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 08:05:51 +0800 Subject: [PATCH 01/73] setting tweets#index routes --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 90856d4fe..5552ba25a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,5 +3,6 @@ devise_for :users # 請依照專案指定規格來設定路由 + root "tweets#index" end From 8b85c97367e175a2b3db69270ede0ada840dea65 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 08:11:17 +0800 Subject: [PATCH 02/73] add tweets index action and view for public users --- app/views/tweets/index.html.erb | 1 + 1 file changed, 1 insertion(+) create mode 100644 app/views/tweets/index.html.erb diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb new file mode 100644 index 000000000..8796a0ec3 --- /dev/null +++ b/app/views/tweets/index.html.erb @@ -0,0 +1 @@ +

前台

\ No newline at end of file From b195ee375d539027d2d870fc2dab5d30eed8c5bb Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 08:23:49 +0800 Subject: [PATCH 03/73] add tweet index action and view for admin --- app/views/admin/tweets/index.html.erb | 1 + config/routes.rb | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 app/views/admin/tweets/index.html.erb diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb new file mode 100644 index 000000000..1199558b7 --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1 @@ +

後台

\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 5552ba25a..10f9a7547 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,4 +5,8 @@ # 請依照專案指定規格來設定路由 root "tweets#index" + namespace :admin do + root "tweets#index" + end + end From 037aeaa07143bc2d236f11850850f22ca1abb162 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 08:55:30 +0800 Subject: [PATCH 04/73] add login process and update view with login message --- app/controllers/admin/tweets_controller.rb | 3 +++ app/controllers/application_controller.rb | 4 ++++ app/controllers/tweets_controller.rb | 2 ++ app/views/layouts/application.html.erb | 7 +++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..d44d8b90a 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,4 +1,7 @@ class Admin::TweetsController < Admin::BaseController + before_action :authenticate_user! + before_action :authenticate_admin + def index end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..0e8d9f3dc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,11 @@ class ApplicationController < ActionController::Base + protect_from_forgery with: :exception + # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # https://github.com/plataformatec/devise#strong-parameters # 注意有 sign_up 和 account_update 兩種參數要處理 + + end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..25533d9a6 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,6 @@ class TweetsController < ApplicationController + before_action :authenticate_user! + def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 952cb7a1b..49c71abb6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -6,6 +6,10 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> + +

<%= notice %>

+

<%= alert %>

+ @@ -21,8 +25,7 @@
  • <%= link_to('註冊', new_user_registration_path) %>
  • <%= link_to('登入', new_user_session_path) %>
  • <% end %> -

    <%= notice %>

    -

    <%= alert %>

    + <%= yield %> From 9fa987fcea085781ad18532021f89347fb369166 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 09:03:31 +0800 Subject: [PATCH 05/73] set up admin authentication --- app/controllers/application_controller.rb | 9 +++++++++ app/models/user.rb | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0e8d9f3dc..dc84a5764 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,5 +7,14 @@ class ApplicationController < ActionController::Base # https://github.com/plataformatec/devise#strong-parameters # 注意有 sign_up 和 account_update 兩種參數要處理 + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "Not allow!" + redirect_to root_path + end + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..041572d13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,4 +11,9 @@ class User < ApplicationRecord validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + + def admin? + self.role == "admin" + end + end From 6ecfa39dacd004e957b576640266d1dc5486d66f Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 09:41:00 +0800 Subject: [PATCH 06/73] add user name to registration form --- app/controllers/application_controller.rb | 10 +++++++++- app/views/devise/confirmations/new.html.erb | 7 ++++++- app/views/devise/registrations/edit.html.erb | 7 ++++++- app/views/devise/registrations/new.html.erb | 7 ++++++- app/views/devise/sessions/new.html.erb | 2 +- app/views/devise/unlocks/new.html.erb | 2 +- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index dc84a5764..a97a5a099 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,15 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :configure_permitted_parameters, if: :devise_controller? + before_action :authenticate_user! + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) + devise_parameter_sanitizer.permit(:account_update, keys: [:name]) + end # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 @@ -16,5 +25,4 @@ def authenticate_admin end end - end diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/devise/confirmations/new.html.erb index 2dea36607..be34ccf6b 100644 --- a/app/views/devise/confirmations/new.html.erb +++ b/app/views/devise/confirmations/new.html.erb @@ -3,9 +3,14 @@ <%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %> <%= devise_error_messages! %> +
    + <%= f.label :name %>
    + <%= f.text_field :name,autofoucs: true %> +
    +
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true, autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %> + <%= f.email_field :email,autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
    diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 10ed32a9e..63fca694f 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,9 +3,14 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %> +
    + <%= f.label :name %>
    + <%= f.text_field :name, autofoucs: true %> +
    +
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 602803cff..c76e9bc5d 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,9 +3,14 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> +
    + <%= f.label :name %>
    + <%= f.text_field :name,autofoucs: true %> +
    +
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
    diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 3ebb001d1..a85adcb34 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -3,7 +3,7 @@ <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
    diff --git a/app/views/devise/unlocks/new.html.erb b/app/views/devise/unlocks/new.html.erb index cfe8aa816..3a04b87b0 100644 --- a/app/views/devise/unlocks/new.html.erb +++ b/app/views/devise/unlocks/new.html.erb @@ -5,7 +5,7 @@
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
    From 9a9e30cbdaafd8b5258502c09ff2dcc9903cc0cb Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 10:15:20 +0800 Subject: [PATCH 07/73] add tweets resources for admin --- app/views/layouts/application.html.erb | 1 - config/routes.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 49c71abb6..2f25b7c7b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -19,7 +19,6 @@ <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • diff --git a/config/routes.rb b/config/routes.rb index 10f9a7547..d271b722a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ root "tweets#index" namespace :admin do + resources :tweets, only: :destroy root "tweets#index" end From 7b4f9f00b58730380c862a4c8b5da638842f5a2b Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 13:55:44 +0800 Subject: [PATCH 08/73] add tweet destroy action for admin --- app/controllers/admin/tweets_controller.rb | 5 ++++ app/views/admin/tweets/index.html.erb | 29 +++++++++++++++++++++- app/views/layouts/application.html.erb | 2 +- config/routes.rb | 2 +- db/seeds.rb | 4 +++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index d44d8b90a..17326deb7 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -3,8 +3,13 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_admin def index + @tweets = Tweet.all end def destroy + @tweet = Tweet.find(params[:id]) + @tweet.destroy + render_to admin_tweets_path + fiash[:alert] = "tweet was delete" end end diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 1199558b7..261cd9a05 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1 +1,28 @@ -

    後台

    \ No newline at end of file +
    + +

    後台

    + +
    + +
    +
    + + + + + <% @tweets.each do |tweet| %> + + + + + + <% end %> + +
    <%= tweet.id %><%= tweet.name %> + <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: { confirm: "Are you sure?"} %> +
    + +
    + +
    +
    \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2f25b7c7b..9b7b6101f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,7 +15,7 @@ <% if current_user %> <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_restaurants_path %>
  • +
  • <%= link_to 'Admin Panel', admin_tweets_path %>
  • <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • diff --git a/config/routes.rb b/config/routes.rb index d271b722a..7eef3dc2d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ root "tweets#index" namespace :admin do - resources :tweets, only: :destroy + resources :tweets, only: [:index, :destroy] root "tweets#index" end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2acc..397580cd3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,7 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + + +User.create (email: "roo@example.com", password:"111111", role: "admin") +puts "Default admin created!" From 58f34b9cb002e4d5778524996359a592d129289f Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 14:24:14 +0800 Subject: [PATCH 09/73] generate fake data using FFaker gem --- app/controllers/admin/tweets_controller.rb | 8 ++++++++ app/models/tweet.rb | 1 + db/seeds.rb | 2 +- lib/tasks/dev.rake | 4 ++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 17326deb7..25827a9f3 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -2,6 +2,7 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_user! before_action :authenticate_admin + def index @tweets = Tweet.all end @@ -12,4 +13,11 @@ def destroy render_to admin_tweets_path fiash[:alert] = "tweet was delete" end + + private + + def restaurant_params + params.require(:tweet).permit(:name, :avatar, :introduction) + end + end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6715fada2..5dd599bba 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,4 +1,5 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 + end diff --git a/db/seeds.rb b/db/seeds.rb index 397580cd3..eea27a394 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,5 +7,5 @@ # Character.create(name: 'Luke', movie: movies.first) -User.create (email: "roo@example.com", password:"111111", role: "admin") +User.create(email:"roo@example.com", password:"111111", role:"admin") puts "Default admin created!" diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 9b1e87ae4..7ee5edbf0 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -16,8 +16,8 @@ namespace :dev do ) user.save! - puts user.name end + puts "have created fake tweets" + puts "now have 20 users data" end - end From b3c6efc5ade93f6f8a87e03b8e8791b80cefb075 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 14:33:01 +0800 Subject: [PATCH 10/73] add paginationooooo using Kaminari gem --- Gemfile | 1 + Gemfile.lock | 15 ++++++++++++++- app/controllers/admin/tweets_controller.rb | 2 +- app/views/admin/tweets/index.html.erb | 2 ++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index c1f1ee3e2..99568ece9 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ gem 'carrierwave' # gem 'mini_magick' gem 'ffaker' +gem 'kaminari' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' diff --git a/Gemfile.lock b/Gemfile.lock index e55e9522f..3e98f70e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,18 @@ GEM jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -259,6 +271,7 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + kaminari listen (>= 3.0.5, < 3.2) puma (~> 3.7) rails (~> 5.1.4) @@ -276,4 +289,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 25827a9f3..d54c56fc4 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -4,7 +4,7 @@ class Admin::TweetsController < Admin::BaseController def index - @tweets = Tweet.all + @tweets = Tweet.page(params[:page]).per(10) end def destroy diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 261cd9a05..a358eeebd 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -21,6 +21,8 @@ <% end %> + + <%= paginate @tweets %>
    From f633a8ccca252c93400e00746e020a0250ace8c3 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 14:39:37 +0800 Subject: [PATCH 11/73] move authenticate_user to application_controller.rb --- app/controllers/admin/tweets_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index d54c56fc4..6effaf683 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,5 +1,4 @@ class Admin::TweetsController < Admin::BaseController - before_action :authenticate_user! before_action :authenticate_admin From dddf2bd07dea4b7a6403b6d129304dbe8866942f Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 14:44:57 +0800 Subject: [PATCH 12/73] add Admin::BaseController and move authenticate_admin here --- app/controllers/admin/base_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 4a89583f5..3b33184d3 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,3 +1,13 @@ class Admin::BaseController < ApplicationController + before_action :authenticate_admin + + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "Not allow!" + redirect_to root_path + end + end end From 0bacf4283fceafdd68ef07455cfe16ab88170d65 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 14:53:52 +0800 Subject: [PATCH 13/73] setup tweet resources for public users --- app/controllers/admin/tweets_controller.rb | 2 +- app/controllers/application_controller.rb | 7 ------- config/routes.rb | 1 + 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 6effaf683..d989ffe17 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,5 +1,5 @@ class Admin::TweetsController < Admin::BaseController - before_action :authenticate_admin + def index diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a97a5a099..fd6490f0e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,11 +18,4 @@ def configure_permitted_parameters private - def authenticate_admin - unless current_user.admin? - flash[:alert] = "Not allow!" - redirect_to root_path - end - end - end diff --git a/config/routes.rb b/config/routes.rb index 7eef3dc2d..af914ce8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ devise_for :users # 請依照專案指定規格來設定路由 + resources :tweets, only: [:index, :create] root "tweets#index" namespace :admin do From bf22a9554d4d8ef155738153ac50e3b4585c6388 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 17:06:44 +0800 Subject: [PATCH 14/73] setup route for user profile page --- app/controllers/tweets_controller.rb | 5 +- app/views/layouts/application.html.erb | 68 +++++++++++++++++++------- config/routes.rb | 2 + 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 25533d9a6..3c0df5d6e 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,9 +1,10 @@ class TweetsController < ApplicationController - before_action :authenticate_user! + def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @users = Tweet.all + # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end def create diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9b7b6101f..c90cbc054 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,30 +1,64 @@ - SimpleTwitter + RestaurantForum <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> - -

    <%= notice %>

    -

    <%= alert %>

    - - <% if current_user %> - <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_tweets_path %>
  • - <% end %> -
  • -
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • - <% else %> -
  • <%= link_to('註冊', new_user_registration_path) %>
  • -
  • <%= link_to('登入', new_user_session_path) %>
  • - <% end %> - + + + + <% if flash[:notice] %> +
    +
    + +
    +
    + <% end %> + + <% if flash[:alert] %> +
    +
    +
    <%= alert %>
    +
    +
    + <% end %> + <%= yield %> diff --git a/config/routes.rb b/config/routes.rb index af914ce8c..499e7a729 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,4 +11,6 @@ root "tweets#index" end + resources :users, only: [:show, :edit, :update] + end From 993d9cc56893fb9cab111c26a4f36f981250f524 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 19:10:31 +0800 Subject: [PATCH 15/73] add user show page --- app/controllers/tweets_controller.rb | 4 +- app/controllers/users_controller.rb | 7 +++ app/views/users/show.html.erb | 69 ++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 3c0df5d6e..84e448d95 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -3,11 +3,13 @@ class TweetsController < ApplicationController def index - @users = Tweet.all + @users = Tweet.page(params[:page]).per(10) # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end def create + @restaurant = Restaurant.find(params[:id]) + end def like diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 750e3c6b5..38999650b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,14 @@ class UsersController < ApplicationController def tweets end + def show + @user = User.find(params[:id]) + end + def edit + unless @user == current_user + redirect_to user_path(@user) + end end def update diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 000000000..32300593a --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,69 @@ +
    +
    +
    + + + + +
    + +
    + + +
    + +
    + + + +
    + +
    + + + +
    +
    + +
    + + + +
    +
    + + +
    + + + +
    +
    +
    \ No newline at end of file From 03687e9626ba2b8f79936a173527fc1c099762e8 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 19:21:40 +0800 Subject: [PATCH 16/73] user can update the profile details --- app/controllers/users_controller.rb | 16 +++++++++++-- app/views/users/edit.html.erb | 35 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 app/views/users/edit.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 38999650b..9f30ef808 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,19 +1,21 @@ class UsersController < ApplicationController + before_action :set_user, only: [:show, :edit, :update] def tweets end def show - @user = User.find(params[:id]) end def edit unless @user == current_user - redirect_to user_path(@user) + redirect_to user_path(@user) end end def update + @user.update(user_params) + redirect_to user_path(@user) end def followings @@ -28,4 +30,14 @@ def likes @likes # 基於測試規格,必須講定變數名稱 end + private + + def set_user + @user = User.find(params[:id]) + end + + def user_params + params.require(:user).permit(:name, :introduction, :avatar ) + end + end diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..a1d7b3c46 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,35 @@ +
    +
    + +
    + +

    Edit Profile

    + + <%= form_for @user do |f| %> +
    + <%= f.label :name, "Name" %> + <%= f.text_field :name, class: "form-control" %> +
    + +
    + <%= f.label :introduction, "自我介紹" %> + <%= f.text_area :introduction, class: "form-control" %> +
    + +
    + <%= f.label :avatar, "大頭照" %> + <%= image_tag @user.avatar if @user.avatar? %> + <%= f.file_field :avatar %> +
    + +
    + <%= f.submit "Update", class: "btn btn-primary" %> + <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> +
    + + <% end %> + +
    + +
    +
    \ No newline at end of file From 8a2f02369ed034026caf467b266ad34f54b5ffa5 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 19:36:35 +0800 Subject: [PATCH 17/73] install bootstrap-sass and setup scss --- Gemfile | 2 ++ Gemfile.lock | 6 ++++++ .../{application.css => application.scss} | 5 +++-- app/views/users/edit.html.erb | 15 +++++++++------ 4 files changed, 20 insertions(+), 8 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (92%) diff --git a/Gemfile b/Gemfile index 99568ece9..ce7fb2c0f 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,8 @@ gem 'carrierwave' gem 'ffaker' gem 'kaminari' +gem 'bootstrap-sass','~>3.3.7' +gem 'sass-rails','~>5.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' diff --git a/Gemfile.lock b/Gemfile.lock index 3e98f70e4..a7392cdeb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -41,11 +41,16 @@ GEM addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) arel (8.0.0) + autoprefixer-rails (9.0.0) + execjs bcrypt (3.1.11) bcrypt (3.1.11-java) bcrypt (3.1.11-x64-mingw32) bcrypt (3.1.11-x86-mingw32) bindex (0.5.0) + bootstrap-sass (3.3.7) + autoprefixer-rails (>= 5.2.1) + sass (>= 3.3.4) builder (3.2.3) byebug (10.0.0) capybara (2.17.0) @@ -263,6 +268,7 @@ PLATFORMS x86-mswin32 DEPENDENCIES + bootstrap-sass (~> 3.3.7) byebug capybara (~> 2.13) carrierwave diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 92% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index d05ea0f51..6788ecd53 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,7 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * - *= require_tree . - *= require_self + */ + @import "bootstrap-sprockets"; + @import "bootstrap"; diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index a1d7b3c46..d56753281 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -6,6 +6,14 @@

    Edit Profile

    <%= form_for @user do |f| %> + +
    + <%= f.label :avatar, "大頭照" %> + <%= image_tag @user.avatar if @user.avatar? %> + <%= f.file_field :avatar %> +
    + +
    <%= f.label :name, "Name" %> <%= f.text_field :name, class: "form-control" %> @@ -16,12 +24,7 @@ <%= f.text_area :introduction, class: "form-control" %>
    -
    - <%= f.label :avatar, "大頭照" %> - <%= image_tag @user.avatar if @user.avatar? %> - <%= f.file_field :avatar %> -
    - +
    <%= f.submit "Update", class: "btn btn-primary" %> <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> From 5aac9466fd9253f554f645a252294e07ba2d57e1 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 19:41:05 +0800 Subject: [PATCH 18/73] install gem 'jquery-rails' for bootstrap js --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/assets/javascripts/application.js | 2 ++ 3 files changed, 8 insertions(+) diff --git a/Gemfile b/Gemfile index ce7fb2c0f..f6abb26f8 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'ffaker' gem 'kaminari' gem 'bootstrap-sass','~>3.3.7' gem 'sass-rails','~>5.0' +gem 'jquery-rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' diff --git a/Gemfile.lock b/Gemfile.lock index a7392cdeb..222777993 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -102,6 +102,10 @@ GEM jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) + jquery-rails (4.3.3) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) kaminari (1.1.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.1.1) @@ -277,6 +281,7 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + jquery-rails kaminari listen (>= 3.0.5, < 3.2) puma (~> 3.7) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..38d5802a2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,3 +13,5 @@ //= require rails-ujs //= require turbolinks //= require_tree . +//= require jquery +//= require bootstrap-sprockets From dedb38e029746553d4aa00a3c11a0fa8208adda5 Mon Sep 17 00:00:00 2001 From: yungcheng Date: Sun, 22 Jul 2018 19:48:39 +0800 Subject: [PATCH 19/73] change navbar --- app/views/layouts/application.html.erb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c90cbc054..f0a9fb437 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,14 +9,18 @@ +

    <%= notice %>

    +

    <%= alert %>