Devise의 User 테이블에서 Update 작업으로 열을 업데이트하려고 할 때 작동하지 않는 현상에 대해
일의 발단
Devise로 만든 User 모델의 테이블에 열을 업데이트하려고 하면 할 수 없었던 것이 시작입니다.
의외의 곳에서 막혔다고 생각했기 때문에 건망록으로 정리합니다.
user_controller.rb
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to user_path(@user.id)
else
render :show
end
end
나는 달의 주행 거리를 마이 페이지에서 추가하고 싶었기 때문에 상세 페이지에 form_with를 갖고 있습니다.
show.html.erb <%= form_with(model: @user, local: true, class: "goal-form") do |f| %>
<%= f.text_field :distance, placeholder: "目標を記入する", class: "form__text" %>
<%= f.submit "設定する", class: "btn btn-primary" %>
<% end %>
보내져 오는 값도 틀리지 않았기 때문에 어째서 야넨이라고 생각하고 있었던 곳이었습니다.
그래서 다음과 같은 오류를 발견했습니다.

원래 User 테이블의 편집에는 Password의 입력이 필요하다고 하는 것.
몰랐어요.
지금까지 사용자를 편집하지 않았습니다.
그래서 비밀번호를 입력하지 않고 사용자를 편집하는 방법을 찾았습니다.
컬럼을 갱신하려면 새로운 컨트롤러의 작성이나 메소드가 필요한 것 같습니다.
먼저 users/registrations_controller.rb를 만듭니다.
registrations_controller.rbclass Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_account_update_params, only: [:update]
protected
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
그런 다음 라우팅을 설정합니다.
route.rb devise_for :users, controllers: {
registrations: 'users/registrations'
}
User 모델 편집. 메서드를 추가합니다.
user.rb
メソッドを追加
def update_without_current_password(params, *options)
params.delete(:current_password)
if params[:password].blank? && params[:password_confirmation].blank?
params.delete(:password)
params.delete(:password_confirmation)
end
result = update_attributes(params, *options)
clean_up_passwords
result
end
end
User 모델에 정의한 메서드를 호출합니다.
registrations_controller.rbclass Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_account_update_params, only: [:update]
protected
追加
def update_resource(resource, params)
resource.update_without_password(params)
end
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:distance])
end
또, 모델에 password의 밸리데이션이 걸려 있으면 아직 에러가 된다고 생각하므로 제외해 둡시다.
user.rb with_options presence: true do
validates :nickname
validates :email
validates :password ←消す
end
할 수 있었습니다. 거의 copipe입니다 ...
여기서 처음 보았던 분들이 많았기 때문에 정리해 보았습니다.
·:account_update···Update를 할 때 지정하는 인수.
・blank?・・・내용이 하늘 혹은 존재하지 않을 때에 true를 돌려준다.
· update_attributes ... 하나의 열만 변경할 수 있습니다. 그러나 유효성 검사가 통과되기 때문에 오류의 판단 위치가 없습니다. (이것으로 고통받았다)
키리가 없기 때문에 여기까지.
완성
앞으로 js에서 오늘 달린 거리가 끌려 줄어가는 기능을 도입하고 싶습니다.
수고하셨습니다.
참고문헌
Reference
이 문제에 관하여(Devise의 User 테이블에서 Update 작업으로 열을 업데이트하려고 할 때 작동하지 않는 현상에 대해), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Tomo_mikoki1104/items/96bb6bdd6be2c6fa9240
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to user_path(@user.id)
else
render :show
end
end
<%= form_with(model: @user, local: true, class: "goal-form") do |f| %>
<%= f.text_field :distance, placeholder: "目標を記入する", class: "form__text" %>
<%= f.submit "設定する", class: "btn btn-primary" %>
<% end %>
몰랐어요.
지금까지 사용자를 편집하지 않았습니다.
그래서 비밀번호를 입력하지 않고 사용자를 편집하는 방법을 찾았습니다.
컬럼을 갱신하려면 새로운 컨트롤러의 작성이나 메소드가 필요한 것 같습니다.
먼저 users/registrations_controller.rb를 만듭니다.
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_account_update_params, only: [:update]
protected
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
그런 다음 라우팅을 설정합니다.
route.rb
devise_for :users, controllers: {
registrations: 'users/registrations'
}
User 모델 편집. 메서드를 추가합니다.
user.rb
メソッドを追加
def update_without_current_password(params, *options)
params.delete(:current_password)
if params[:password].blank? && params[:password_confirmation].blank?
params.delete(:password)
params.delete(:password_confirmation)
end
result = update_attributes(params, *options)
clean_up_passwords
result
end
end
User 모델에 정의한 메서드를 호출합니다.
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_account_update_params, only: [:update]
protected
追加
def update_resource(resource, params)
resource.update_without_password(params)
end
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:distance])
end
또, 모델에 password의 밸리데이션이 걸려 있으면 아직 에러가 된다고 생각하므로 제외해 둡시다.
user.rb
with_options presence: true do
validates :nickname
validates :email
validates :password ←消す
end
할 수 있었습니다. 거의 copipe입니다 ...
여기서 처음 보았던 분들이 많았기 때문에 정리해 보았습니다.
·:account_update···Update를 할 때 지정하는 인수.
・blank?・・・내용이 하늘 혹은 존재하지 않을 때에 true를 돌려준다.
· update_attributes ... 하나의 열만 변경할 수 있습니다. 그러나 유효성 검사가 통과되기 때문에 오류의 판단 위치가 없습니다. (이것으로 고통받았다)
키리가 없기 때문에 여기까지.
완성
앞으로 js에서 오늘 달린 거리가 끌려 줄어가는 기능을 도입하고 싶습니다.
수고하셨습니다.
참고문헌
Reference
이 문제에 관하여(Devise의 User 테이블에서 Update 작업으로 열을 업데이트하려고 할 때 작동하지 않는 현상에 대해), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Tomo_mikoki1104/items/96bb6bdd6be2c6fa9240
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Devise의 User 테이블에서 Update 작업으로 열을 업데이트하려고 할 때 작동하지 않는 현상에 대해), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Tomo_mikoki1104/items/96bb6bdd6be2c6fa9240텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)