사용자 소개 업데이트 오류
4628 단어 Rails
프로필 페이지에 닉네임, 자기소개 편집 기능 추가
routes.rb
resources :users, only: [:show, :new, :edit, :create, :destroy, :update]
users_contoroller def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:notice] = "変更しました。"
redirect_to "/users/#{@user.id}/edit"
else
flash[:alert] = "失敗しました"
redirect_to "/users/#{@user.id}/edit"
end
end
private
def user_params
params.require(:user).permit(:nickname,:introduce)
end
edit.html.haml.wrapper1.clearfix
= render 'mypage/mypage-sidemenu'
.profile-container
%h2.container__profile プロフィール
= form_for @user, url:"/users/#{@user.id}" , method: :patch, html:{class: "edit_form"} do |f|
.container__name
= image_tag "https://static.mercdn.net/images/member_photo_noimage_thumb.png", class: "image-tag"
= f.text_field :nickname, value: "#{@user.nickname}" ,class: "edit_name"
.container__text
= f.text_area :introduce, value: "#{@user.introduce}",class: "introduce", placeholder: "例)こんにちは☆ ご覧いただきありがとうございます!かわいいものやきれいめオフィスカジュアル中心に出品しています。服のサイズは主に9号です。気持ちよくお取引できるよう心がけていますので、商品や発送方法などご質問がありましたらお気軽にどうぞ♪"
.container__bottun
= f.submit "変更する", class: 'change-profile-form__action-btn'
form_with면 왜 틀린지 몰라 formfor를 만들다.
위에서 말한 바와 같이 업데이트할 수 없습니다.
controller binding.pry, @user를 끼고 있습니다.valid?→@user.errors.full_메시지를 만든 후 비밀번호가 필요합니다.(콘솔에서 current user가 로그인 상태로 확인됨)
다른 페이지 (사용자 새 로그인) 의validation이 작용했습니다.
user.rb # バリデーション
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_PHONE_REGEX = /\A\d{10}$|^\d{11}\z/
VALID_ADDRESS_NUMBER_REGEX = /\A[0-9]{3}-[0-9]{4}\z/
validates :nickname, presence: true, length: {maximum: 20}
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX }
validates :password, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password_confirmation, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :password_confirmation, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :last_name, presence: true
validates :first_name, presence: true
validates :last_name_kana, presence: true
validates :first_name_kana, presence: true
validates :birthdate_year, numericality: true
validates :birthdate_month, numericality: true
validates :birthdate_day, numericality: true
validates :phone_number, presence: true, uniqueness: true, format: { with: VALID_PHONE_REGEX }
validates :address_last_name, presence: true
validates :address_first_name, presence: true
validates :address_last_name_kana, presence: true
validates :address_first_name_kana, presence: true
validates :address_number, presence: true, format: { with: VALID_ADDRESS_NUMBER_REGEX }
validates :address_prefecture, presence: true
validates :address_name, presence: true
validates :address_block, presence: true
validates :address_phone_number, allow_blank: true, format: { with: VALID_PHONE_REGEX }
end
다음 변경 사항.
user.rbvalidates :password, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password_confirmation, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :password_confirmation, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validation 추가, on: 동작 이름에서 이validation을 사용하는 장면을 지정합니다.
페이지에서 allow 편집blank이기 때문에 공백(미입력)에서 먼저 편집을 진행하면 편집할 수 있습니다.
Reference
이 문제에 관하여(사용자 소개 업데이트 오류), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/nyohon22/items/4710638e425262f41002
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
resources :users, only: [:show, :new, :edit, :create, :destroy, :update]
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:notice] = "変更しました。"
redirect_to "/users/#{@user.id}/edit"
else
flash[:alert] = "失敗しました"
redirect_to "/users/#{@user.id}/edit"
end
end
private
def user_params
params.require(:user).permit(:nickname,:introduce)
end
.wrapper1.clearfix
= render 'mypage/mypage-sidemenu'
.profile-container
%h2.container__profile プロフィール
= form_for @user, url:"/users/#{@user.id}" , method: :patch, html:{class: "edit_form"} do |f|
.container__name
= image_tag "https://static.mercdn.net/images/member_photo_noimage_thumb.png", class: "image-tag"
= f.text_field :nickname, value: "#{@user.nickname}" ,class: "edit_name"
.container__text
= f.text_area :introduce, value: "#{@user.introduce}",class: "introduce", placeholder: "例)こんにちは☆ ご覧いただきありがとうございます!かわいいものやきれいめオフィスカジュアル中心に出品しています。服のサイズは主に9号です。気持ちよくお取引できるよう心がけていますので、商品や発送方法などご質問がありましたらお気軽にどうぞ♪"
.container__bottun
= f.submit "変更する", class: 'change-profile-form__action-btn'
user.rb
# バリデーション
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_PHONE_REGEX = /\A\d{10}$|^\d{11}\z/
VALID_ADDRESS_NUMBER_REGEX = /\A[0-9]{3}-[0-9]{4}\z/
validates :nickname, presence: true, length: {maximum: 20}
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX }
validates :password, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password_confirmation, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :password_confirmation, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :last_name, presence: true
validates :first_name, presence: true
validates :last_name_kana, presence: true
validates :first_name_kana, presence: true
validates :birthdate_year, numericality: true
validates :birthdate_month, numericality: true
validates :birthdate_day, numericality: true
validates :phone_number, presence: true, uniqueness: true, format: { with: VALID_PHONE_REGEX }
validates :address_last_name, presence: true
validates :address_first_name, presence: true
validates :address_last_name_kana, presence: true
validates :address_first_name_kana, presence: true
validates :address_number, presence: true, format: { with: VALID_ADDRESS_NUMBER_REGEX }
validates :address_prefecture, presence: true
validates :address_name, presence: true
validates :address_block, presence: true
validates :address_phone_number, allow_blank: true, format: { with: VALID_PHONE_REGEX }
end
다음 변경 사항.user.rb
validates :password, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password_confirmation, presence: true, length: {minimum: 6, maximum: 128},on: :save_to_session_before_phone
validates :password, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validates :password_confirmation, length: {minimum: 6, maximum: 128},on: :update,allow_blank: true
validation 추가, on: 동작 이름에서 이validation을 사용하는 장면을 지정합니다.페이지에서 allow 편집blank이기 때문에 공백(미입력)에서 먼저 편집을 진행하면 편집할 수 있습니다.
Reference
이 문제에 관하여(사용자 소개 업데이트 오류), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/nyohon22/items/4710638e425262f41002텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)