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/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 5b0bbb8..99e98a6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,4 +8,26 @@ 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 + return unless current_user.id != params[:id].to_i + + redirect_to user_path(current_user), flash: { danger: '自分以外のプロフィールは編集できません。' } + nil + end + + def update + current_user.attributes = user_params + if current_user.save(context: :not_new_form) + redirect_to user_path(current_user) + else + render :edit + 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/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/edit.html.slim b/app/views/users/edit.html.slim new file mode 100644 index 0000000..ae1674d --- /dev/null +++ b/app/views/users/edit.html.slim @@ -0,0 +1,32 @@ +.container-fluid + = render 'shared/navigation' + .row.flex-nowrap + = render 'shared/sidebar', :user => current_user + .col + = 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, "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" + .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" + .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), 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..bc0de8b 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -14,26 +14,28 @@ = 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 = @user.name span.text-muted.d-block.mb-2 = "\@#{@user.user_name}" - span - = @user.introduction + - if @user.introduction? + span + = safe_join(@user.introduction.split("\n"),tag(:br)) .container-grid.w-75.text-muted .row - - if @user.place + - if @user.place? .col i.bi-geo-alt.mx-2 = @user.place - - if @user.website + - if @user.website? .col i.bi-link-45deg.mx-2 = @user.website - - if @user.birthdate + - if @user.birthdate? .col i.bi-balloon.mx-2 | 誕生日: 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 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