사용자를 관리하는 동안 관리자가 사용자의 devise user.confirm 메소드를 트리거하는 확인란을 사용하여 사용자의 계정을 확인할 수 있기를 바랍니다.
처음에는 attr_accessor를 사용하여 사용자 양식에서 부울로 사용할 confirm_now 라는 변수를 설정할 수 있다고 생각한 다음 컨트롤러 업데이트 작업 (또는 작업 전 / 후 콜백에 의해)에서 부울을 평가하고 사용자를 확인합니다. 따라서.
attr_accessor 변수를 설정하고 읽기 위해 모델에 메서드가 필요한지 아직 확실하지 않습니다. 여전히 그것에 대해 머리를 찟고 있습니다 ... 또는 아마도 이것을 지나치게 복잡하게 만들고 사용자에게 전화해야합니다. 저장을 클릭 할 때 메소드를 사용합니다 (나쁜 습관입니까?-user_params에이 필드를 추가하는 것을 망설임).
모델:
class User < ApplicationRecord
attr_accessor :confirm_now
...
제어 장치:
class UsersController < ApplicationController
...
def update
if params[:user][:password].blank?
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
if [email protected]? && @user.confirm_now
@user.confirm
end
...
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :account_active, :confirm_now)
end
양식 스 니펫 (예상대로 평가되고 작동 함 ) :
<% if [email protected]? %>
<%= f.input :confirm_now, label: 'Confirm User', as: :boolean, checked_value: true, unchecked_value: false %>
<% end %>
Rails Log (attr_accessor는 양식에서 확인할 때 설정 됨) :
Processing by UsersController#update as HTML
Parameters: {"authenticity_token"=>"...", "user"=>{"first_name"=>"gg", "last_name"=>"gg", "email"=>"[email protected]", "account_active"=>"1", "confirm_now"=>"true", "password"=>"[FILTERED]"}, "commit"=>"Save", "id"=>"45"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."first_name" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."first_name" ASC LIMIT $2 [["id", 45], ["LIMIT", 1]]
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."first_name" ASC LIMIT $2 [["id", 45], ["LIMIT", 1]]
Redirected to http://localhost:3000/users
Completed 302 Found in 6ms (ActiveRecord: 0.6ms | Allocations: 2665)
미리 감사드립니다!
단순화하기 위해 버튼으로 만들고 confirmed_at
필드를 직접 전달합니다 .
매개 변수 허용 목록 :
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :account_active, :confirmed_at)
end
형태:
<%=
if @user.confirmed?
link_to 'Unconfirm user',
user_path(@user, { user: { confirmed_at: nil } }),
method: :patch
else
link_to 'Confirm user',
user_path(@user, { user: { confirmed_at: Time.current } }),
method: :patch
end
%>
경로에 따라 경로 / 방법 유형을 조정하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다