From 2b7e41747e7a3c5c7e8e189dbd94001fb477573f Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 12:57:59 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=E7=B7=A8=E9=9B=86=E7=94=BB=E9=9D=A2?= =?UTF-8?q?=E3=81=AE=E3=81=B2=E3=81=AA=E5=BD=A2=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 23 +++++++++++++++++++++++ app/views/users/edit.html.slim | 29 +++++++++++++++++++++++++++++ config/locales/devise.views.ja.yml | 5 +++++ config/routes.rb | 2 +- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 app/views/users/edit.html.slim diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5b0bbb8..1311482 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,4 +8,27 @@ def show @reposting_posts = @user.reposting_posts.includes(:user).latest.page(params[:repost_page]).per(10) @commenting_posts = @user.commenting_posts.includes(:user).latest.page(params[:comment_page]).per(10) end + + def edit + if params[:id].to_i != current_user.id + p 'current_user.id' + p current_user.id + redirect_to root_path, flash: { danger: '自分以外のプロフィールは編集できません。' } + return + end + end + + def update + if current_user.update(user_params) + return + else + return + end + end + + private + + def user_params + params.require(:user).permit(%i[name introduction place website birthdate photo header_photo]) + end end diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim new file mode 100644 index 0000000..db55e33 --- /dev/null +++ b/app/views/users/edit.html.slim @@ -0,0 +1,29 @@ +.container-fluid + = render 'shared/navigation' + .row.flex-nowrap + = render 'shared/sidebar', :user => current_user + .col + = render 'shared/flash' + h3.fw-bold.mt-2 + | プロフィールを編集 + = form_with model: current_user, url: user_path(current_user), data: { turbo: false } do |f| + .form-group.my-3 + = f.label :name, class: "mb-2 block text-sm text-gray-600" + = f.text_field :name, class: "form-control" + .form-group.my-3 + = f.label :introduction, class: "mb-2 block text-sm text-gray-600" + = f.text_area :introduction, class: "form-control" + .form-group.my-3 + = f.label :place, class: "mb-2 block text-sm text-gray-600" + = f.text_field :place, class: "form-control" + .form-group.my-3 + = f.label :website, class: "mb-2 block text-sm text-gray-600" + = f.text_field :website, class: "form-control" + .form-group.my-3 + = f.label :photo, class: "mb-2 block text-sm text-gray-600" + = f.file_field :photo, class: "form-control" + .text-center.my-3 + = link_to "戻る", user_path(current_user), class: "btn btn-secondary mt-auto" + button.btn.btn-primary[type="submit"] + | 保存 + diff --git a/config/locales/devise.views.ja.yml b/config/locales/devise.views.ja.yml index ebb5077..86470df 100644 --- a/config/locales/devise.views.ja.yml +++ b/config/locales/devise.views.ja.yml @@ -29,6 +29,11 @@ ja: user_name: ユーザー名 phone: 電話番号 birthdate: 生年月日 + introduction: 自己紹介 + place: 場所 + website: ウェブサイト + photo: プロフィール画像 + header_photo: ヘッダー画像 models: user: ユーザー devise: diff --git a/config/routes.rb b/config/routes.rb index 03bbb8f..2e1a15f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,7 @@ } resources :posts, only: %i[index] - resources :users, only: %i[show] + resources :users, only: %i[show edit update] root 'posts#index' end From 4ce121a3dd2e9132e82db1bba00f5912aa248aea Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 13:42:25 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=E7=B7=A8=E9=9B=86=E7=94=BB=E9=9D=A2?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E8=A1=A8=E7=A4=BA=E3=80=81=E3=83=9C?= =?UTF-8?q?=E3=82=BF=E3=83=B3=E3=81=A7=E9=81=B7=E7=A7=BB=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 6 ++---- app/views/users/edit.html.slim | 10 +++++++--- app/views/users/show.html.slim | 3 ++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1311482..a781604 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,8 +11,6 @@ def show def edit if params[:id].to_i != current_user.id - p 'current_user.id' - p current_user.id redirect_to root_path, flash: { danger: '自分以外のプロフィールは編集できません。' } return end @@ -20,9 +18,9 @@ def edit def update if current_user.update(user_params) - return + redirect_to request.referer, flash: { success: 'プロフィールを更新しました。' } else - return + redirect_to request.referer, flash: { danger: 'プロフィール更新失敗' } end end diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim index db55e33..b3a6216 100644 --- a/app/views/users/edit.html.slim +++ b/app/views/users/edit.html.slim @@ -4,9 +4,10 @@ = render 'shared/sidebar', :user => current_user .col = render 'shared/flash' + = render "users/shared/error_messages", resource: current_user h3.fw-bold.mt-2 | プロフィールを編集 - = form_with model: current_user, url: user_path(current_user), data: { turbo: false } do |f| + = form_with model: current_user, url: user_path(current_user), data: { turbo: false, "turbo-method": :put } do |f| .form-group.my-3 = f.label :name, class: "mb-2 block text-sm text-gray-600" = f.text_field :name, class: "form-control" @@ -22,8 +23,11 @@ .form-group.my-3 = f.label :photo, class: "mb-2 block text-sm text-gray-600" = f.file_field :photo, class: "form-control" + .form-group.my-3 + = f.label :header_photo, class: "mb-2 block text-sm text-gray-600" + = f.file_field :header_photo, class: "form-control" .text-center.my-3 - = link_to "戻る", user_path(current_user), class: "btn btn-secondary mt-auto" - button.btn.btn-primary[type="submit"] + = link_to "戻る", user_path(current_user), data: { turbo: false }, class: "btn btn-outline-dark mx-1 fw-bold" + button.btn.btn-dark.mt-auto.text-white.fw-bold[type="submit"] | 保存 diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index 0ecf59d..d9e2f02 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -14,7 +14,8 @@ = image_tag @user.header_photo, class: "upper" - if @user.photo.attached? = image_tag @user.photo, class: "rounded-circle profile" - .mt-lg-5 + .text-end + = link_to "プロフィールを編集する", edit_user_path(current_user), data: { turbo: false, "turbo-method": :post }, class: "btn btn-outline-dark m-2 fw-bold" .profile-content .mt-5 h1.mb-0 From f3b90d55059413f1842c6b67d9c3e026275bdc55 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 14:19:22 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=E7=94=BB=E5=83=8F=E4=BB=A5=E5=A4=96?= =?UTF-8?q?=E3=81=AE=E6=9B=B4=E6=96=B0=E5=87=A6=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users/registrations_controller.rb | 7 +++---- app/controllers/users_controller.rb | 3 ++- app/views/users/show.html.slim | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 60c6ffc..9ef49ce 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -3,7 +3,6 @@ module Users class RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] - before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up # def new @@ -47,9 +46,9 @@ def configure_sign_up_params end # If you have extra params to permit, append them to the sanitizer. - def configure_account_update_params - devise_parameter_sanitizer.permit(:account_update, keys: %i[phone birthdate name user_name]) - end + # def configure_account_update_params + # devise_parameter_sanitizer.permit(:account_update, keys: %i[phone birthdate name user_name]) + # end # The path used after sign up. # def after_sign_up_path_for(resource) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a781604..fb46648 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -17,7 +17,8 @@ def edit end def update - if current_user.update(user_params) + current_user.attributes = user_params + if current_user.save(context: :omniauth) redirect_to request.referer, flash: { success: 'プロフィールを更新しました。' } else redirect_to request.referer, flash: { danger: 'プロフィール更新失敗' } diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index d9e2f02..91083cf 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -23,7 +23,7 @@ span.text-muted.d-block.mb-2 = "\@#{@user.user_name}" span - = @user.introduction + = safe_join(@user.introduction.split("\n"),tag(:br)) .container-grid.w-75.text-muted .row - if @user.place From d4520814139273ad830b9a7b5fffa73acbc0a995 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 14:24:05 +0900 Subject: [PATCH 4/8] =?UTF-8?q?user=E3=81=AEvalidates=E3=81=AEcontext?= =?UTF-8?q?=E3=82=92rename?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users/omniauth_callbacks_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- app/models/user.rb | 4 ++-- app/views/users/show.html.slim | 5 +++-- db/seeds.rb | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 79210ea..24fb856 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -10,7 +10,7 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController def github @user = User.from_omniauth(request.env['omniauth.auth']) @user.skip_confirmation! - @user.save!(context: :omniauth) + @user.save!(context: :not_new_form) if @user.persisted? sign_in_and_redirect @user, event: :authentication set_flash_message(:notice, :success, kind: 'GitHub') if is_navigational_format? diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fb46648..ca06161 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -18,7 +18,7 @@ def edit def update current_user.attributes = user_params - if current_user.save(context: :omniauth) + if current_user.save(context: :not_new_form) redirect_to request.referer, flash: { success: 'プロフィールを更新しました。' } else redirect_to request.referer, flash: { danger: 'プロフィール更新失敗' } diff --git a/app/models/user.rb b/app/models/user.rb index a9fa3b6..7dbcb86 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,8 +37,8 @@ class User < ApplicationRecord validates :name validates :user_name, uniqueness: true # 電話番号と誕生日はGithubでの新規登録時には無効化する。 - validates :phone, unless: -> { validation_context == :omniauth } - validates :birthdate, unless: -> { validation_context == :omniauth } + validates :phone, unless: -> { validation_context == :not_new_form } + validates :birthdate, unless: -> { validation_context == :not_new_form } end def self.from_omniauth(auth) diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index 91083cf..c72c737 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -22,8 +22,9 @@ = @user.name span.text-muted.d-block.mb-2 = "\@#{@user.user_name}" - span - = safe_join(@user.introduction.split("\n"),tag(:br)) + - if @user.introduction + span + = safe_join(@user.introduction.split("\n"),tag(:br)) .container-grid.w-75.text-muted .row - if @user.place diff --git a/db/seeds.rb b/db/seeds.rb index 650be37..62897c1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -20,7 +20,7 @@ birthdate: '2000-01-01' ) user.skip_confirmation! - user.save!(context: :omniauth) + user.save!(context: :not_new_form) user_ids << user.id end From 0fd6dac59a0f5c2e62cc69144760d7bd2ac97ba5 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 14:41:23 +0900 Subject: [PATCH 5/8] =?UTF-8?q?=E7=B7=A8=E9=9B=86=E6=99=82=E3=81=AE?= =?UTF-8?q?=E9=81=B7=E7=A7=BB=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 4 ++-- app/views/users/edit.html.slim | 1 - app/views/users/show.html.slim | 8 ++++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ca06161..5656e7b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -19,9 +19,9 @@ def edit def update current_user.attributes = user_params if current_user.save(context: :not_new_form) - redirect_to request.referer, flash: { success: 'プロフィールを更新しました。' } + redirect_to user_path(current_user) else - redirect_to request.referer, flash: { danger: 'プロフィール更新失敗' } + render :edit end end diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim index b3a6216..ae1674d 100644 --- a/app/views/users/edit.html.slim +++ b/app/views/users/edit.html.slim @@ -3,7 +3,6 @@ .row.flex-nowrap = render 'shared/sidebar', :user => current_user .col - = render 'shared/flash' = render "users/shared/error_messages", resource: current_user h3.fw-bold.mt-2 | プロフィールを編集 diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index c72c737..6f8cc62 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -22,20 +22,20 @@ = @user.name span.text-muted.d-block.mb-2 = "\@#{@user.user_name}" - - if @user.introduction + - if @user.introduction.present? span = safe_join(@user.introduction.split("\n"),tag(:br)) .container-grid.w-75.text-muted .row - - if @user.place + - if @user.place.present? .col i.bi-geo-alt.mx-2 = @user.place - - if @user.website + - if @user.website.present? .col i.bi-link-45deg.mx-2 = @user.website - - if @user.birthdate + - if @user.birthdate.present? .col i.bi-balloon.mx-2 | 誕生日: From 21ec91258ef852be7a444cad7be3e0f71748fa88 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 14:43:10 +0900 Subject: [PATCH 6/8] =?UTF-8?q?rubocop=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5656e7b..2478462 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,10 +10,10 @@ def show end def edit - if params[:id].to_i != current_user.id - redirect_to root_path, flash: { danger: '自分以外のプロフィールは編集できません。' } - return - end + return unless params[:id].to_i != current_user.id + + redirect_to root_path, flash: { danger: '自分以外のプロフィールは編集できません。' } + nil end def update From f3d14891fb5c2e3ff9974a7a798ebbcba86e26a4 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 14:56:18 +0900 Subject: [PATCH 7/8] =?UTF-8?q?rails=5Fbest=5Fpractices=E5=AF=BE=E5=BF=9Cu?= =?UTF-8?q?se=20scope=20access?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 4 ++-- app/views/users/show.html.slim | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2478462..e025df3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,9 +10,9 @@ def show end def edit - return unless params[:id].to_i != current_user.id + return unless current_user != User.find(params[:id]) - redirect_to root_path, flash: { danger: '自分以外のプロフィールは編集できません。' } + redirect_to user_path(current_user), flash: { danger: '自分以外のプロフィールは編集できません。' } nil end diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index 6f8cc62..bc0de8b 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -22,20 +22,20 @@ = @user.name span.text-muted.d-block.mb-2 = "\@#{@user.user_name}" - - if @user.introduction.present? + - if @user.introduction? span = safe_join(@user.introduction.split("\n"),tag(:br)) .container-grid.w-75.text-muted .row - - if @user.place.present? + - if @user.place? .col i.bi-geo-alt.mx-2 = @user.place - - if @user.website.present? + - if @user.website? .col i.bi-link-45deg.mx-2 = @user.website - - if @user.birthdate.present? + - if @user.birthdate? .col i.bi-balloon.mx-2 | 誕生日: From ab23c5b8735ab24bf4f540ad43646855e1cfbe41 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 20 Sep 2023 15:00:41 +0900 Subject: [PATCH 8/8] =?UTF-8?q?rails=5Fbest=5Fpractices=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e025df3..99e98a6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,7 +10,7 @@ def show end def edit - return unless current_user != User.find(params[:id]) + return unless current_user.id != params[:id].to_i redirect_to user_path(current_user), flash: { danger: '自分以外のプロフィールは編集できません。' } nil