gpt4 book ai didi

ruby-on-rails-4 - 设计更改电子邮件不会更新电子邮件

转载 作者:行者123 更新时间:2023-12-03 23:36:32 24 4
gpt4 key购买 nike

我试图允许用户更改他们的电子邮件地址,这些地址被设计用作他们的唯一用户名。即使更新没有出现错误,也不会更改数据库中用户的电子邮件地址。

以下是代码的相关部分:

形式:

<%= f.fields_for :user_account, @user.user_account do |user_account| %> 
<p>Edit email address for account: <%= @user.user_account.email %></p>
<div class="field">
<%= user_account.label :new_email %><br />
<%= user_account.text_field :email, autocomplete: "off", value: nil %>
</div>
<div class="field">
<%= user_account.label :password %> <i>(please confirm the password associated with this account)</i><br />
<%= user_account.password_field :current_password, autocomplete: "off" %>
</div>
<%= hidden_field_tag 'form', 'email' %>
<div class="actions">
<%= user_account.submit "Edit" %>
</div>



Controller :
def update
respond_to do |format|
if params[:form] == 'email'
if @user.user_account.valid_password?(params[:user][:user_account_attributes][:current_password])
if @user.update(user_params)
format.html { redirect_to user_path(@user), :notice => 'your new email has been saved' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
else
format.html { redirect_to edit_email_path(@user), :notice => 'incorrect password (email)' }
end
else ...

user_params 方法:
def user_params
params.require(:user).permit(
:first_name, :middle_initial, :last_name,
:linkedin, :website, :facebook, :video, :phone_number,
:address_1, :address_2, :city, :zip_code,
:image, :number, :years_practicing, :neighborhood, :biography, :price, :status,
user_employments_attributes: [:id, :user_id, :company, :position, :start_date, :end_date, :info, :_destroy],
user_educations_attributes: [:id, :user_id, :school, :degree_title, :start_date, :end_date, :info, :_destroy],
user_account_attributes: [:id, :user_id, :email, :password, :password_confirmation, :_destroy],
user_category_ids:[])
end

用户帐户模型:
 class UserAccount < ActiveRecord::Base
# Include default devise modules. Others available are:
# , :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :user
end

最佳答案

好的,原来新的电子邮件地址被保存为:unconfirmed_email,但这并没有改变链接帐户的任何功能,因为该帐户仍然将旧电子邮件存储为:email。

因此我不得不这样做

user.confirmed_at = nil
user.save(:validate => false)

为了重新发送用户确认电子邮件,以便登录表单不再接受旧密码。

关于ruby-on-rails-4 - 设计更改电子邮件不会更新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com