sách gpt4 ai đã đi

Thiết kế: Đổi mật khẩu

In lại 作者:行者123 更新时间:2023-12-04 05:57:08 27 4
mua khóa gpt4 Nike

我已经坚持了超过 24 小时,试图按照此处发布的其他解决方案进行操作,但我无法使其正常工作。我是 Rails 新手,需要帮助!

我想让我的/users/edit 页面正常工作,以便我可以简单地更改用户密码。最初,我想在没有 current_password 的情况下执行此操作,但我不介意将其留在那里,只要我可以更改和更新密码即可。

这是我所做的:

我遵循了 Devise Wiki 中的示例并将其插入到我指定从 Devise::RegistrationsController 继承的用户 Controller 中

class UsersController < Devise::RegistrationsController
...
kết thúc

我改变了路线:
devise_for :users, :controllers => { :registrations => 'users' } do 
match '/users' => 'users#index'
kết thúc

这是我的模型:
lớp Người dùng < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

attr_accessor :password, :password_confirmation, :current_password
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :full_name, :coach, :bio

validates :full_name, presence: true

kết thúc

我假设我创建的 UsersController 会覆盖 Registrations Controller ,并且我将能够更改/更新密码。它在重定向到 root_path 的范围内起作用(这只意味着在没有当前密码的情况下更新后发生)但没有保存新密码(我检查了日志并且没有 SQL 显示它已保存)...

Bạn có ý tưởng nào không?

1 Câu trả lời

尝试做类似的事情:
Devise Forgot Password for logged in user

这让您有一个单独的 View 来更改密码。

关键是我无法让它在设计中工作,所以我在用户 Controller 中编写了自己的解决方案并发布到它,而不是使用设计提供的方法。

将此添加到您希望能够编辑密码的用户表单中:

<%= form_for(@user, :url => url_for(:action => :do_reset_password) , :html => { :method => :post }) do |f| %>

<%= f.hidden_field :reset_password_token %>

<%= f.label :password, "New password" %>

<%= f.password_field :password %>


<%= f.label :password_confirmation, "Confirm new password" %>

<%= f.password_field :password_confirmation %>


<%= f.submit "Change my password" %>

<% kết thúc %>

用户 Controller :
    def do_reset_password
id = params[:id]

# there may be a better way of doing this, devise should be able to give us these messages
if params[:user][:password] != params[:user][:password_confirmation]
flash[:alert] = "Passwords must match."
redirect_to :back
trở lại
kết thúc
if @user.reset_password!(params[:user][:password],params[:user][:password_confirmation])
@user.save
respond_to do |format|
format.html { redirect_to '/home', notice: 'Your password has been changed.' }
kết thúc
khác
flash[:alert] = "Invalid password, must be at least 6 charactors."
redirect_to :back
kết thúc
kết thúc

配置/路由.rb
resource :users do
post 'do_reset_password'
kết thúc

关于设计:更改密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388866/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com