Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

プロフィール編集画面 #10

Merged
merged 8 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
# frozen_string_literal: true

class UsersController < ApplicationController
def show

Check notice on line 4 in app/controllers/users_controller.rb

View workflow job for this annotation

GitHub Actions / rails_best_practices

[rails_best_practices] app/controllers/users_controller.rb#L4

move model logic into model (@user use_count > 4)
Raw output
/home/runner/work/twitter_clone/twitter_clone/app/controllers/users_controller.rb:4 - move model logic into model (@user use_count > 4)
@user = User.find(params[:id])
@posts = @user.posts.includes(:user).latest.page(params[:posts_page]).per(10)
@liking_posts = @user.liking_posts.includes(:user).latest.page(params[:like_page]).per(10)
@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

Check notice on line 13 in app/controllers/users_controller.rb

View workflow job for this annotation

GitHub Actions / rails_best_practices

[rails_best_practices] app/controllers/users_controller.rb#L13

use scope access
Raw output
/home/runner/work/twitter_clone/twitter_clone/app/controllers/users_controller.rb:13 - use scope access

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
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions app/views/users/edit.html.slim
Original file line number Diff line number Diff line change
@@ -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"]
| 保存

14 changes: 8 additions & 6 deletions app/views/users/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
| 誕生日:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/devise.views.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ ja:
user_name: ユーザー名
phone: 電話番号
birthdate: 生年月日
introduction: 自己紹介
place: 場所
website: ウェブサイト
photo: プロフィール画像
header_photo: ヘッダー画像
models:
user: ユーザー
devise:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading