Rails 形式:显示一个变量的值但将其保存到另一个变量的字段
Rails form: A field showing the value of one variable but saving it to another variable
我有一个包含用户个人资料数据的编辑表单。 User table 中的两个变量是 email
和 new_email
。我希望编辑表单中的字段之一 show 用户的 email
但如果在该字段中输入新值,我希望它不会 保存到email
而不是new_email
。所以我想我需要一个用于 new_email
的表单域,它显示 email
的值。我应该如何调整下面的代码来实现这一点?
我目前认为:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :email %>
<%= f.email_field :new_email, class: 'form-control' %> #!!Relevant line.
<%= f.label :other_fields_like_this %>
<%= f.text_field :other_fields_like_this, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
控制器:
def update
@user = User.friendly.find(params[:id])
if @user.update_attributes(userupdate_params)
if params[:user][:new_email] != @user.email # That is, if: a new email address is entered, i.e. a value other then the existing value for email is entered
#save value to new_email instead of to email. How to do this?
send_confirmation_email
flash[:success] = "Your email address has not yet been updated. Please check your email for a confirmation link."
redirect_to @user
end
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def userupdate_params
params.require(:user).permit(#:email, #email I think can be removed here, since it's new_email whose value will change.
:new_email,
:other_fields_like_this,
:password,
:password_confirmation)
end
只是为了提供一些背景信息:在此之后我打算用一封确认电子邮件来扩展它。如果用户单击确认 link,则 new_email
的值将覆盖 email
的值。因此,我希望它最初保存到 new_email
,只有在确认后才保存到 email
.
您可以使用 before_update
回调并将其值分配给 new_email
字段。
您可以为输入字段提供默认值:
<%= f.email_field :new_email, value: @user.email, class: 'form-control' %>
我有一个包含用户个人资料数据的编辑表单。 User table 中的两个变量是 email
和 new_email
。我希望编辑表单中的字段之一 show 用户的 email
但如果在该字段中输入新值,我希望它不会 保存到email
而不是new_email
。所以我想我需要一个用于 new_email
的表单域,它显示 email
的值。我应该如何调整下面的代码来实现这一点?
我目前认为:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :email %>
<%= f.email_field :new_email, class: 'form-control' %> #!!Relevant line.
<%= f.label :other_fields_like_this %>
<%= f.text_field :other_fields_like_this, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
控制器:
def update
@user = User.friendly.find(params[:id])
if @user.update_attributes(userupdate_params)
if params[:user][:new_email] != @user.email # That is, if: a new email address is entered, i.e. a value other then the existing value for email is entered
#save value to new_email instead of to email. How to do this?
send_confirmation_email
flash[:success] = "Your email address has not yet been updated. Please check your email for a confirmation link."
redirect_to @user
end
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def userupdate_params
params.require(:user).permit(#:email, #email I think can be removed here, since it's new_email whose value will change.
:new_email,
:other_fields_like_this,
:password,
:password_confirmation)
end
只是为了提供一些背景信息:在此之后我打算用一封确认电子邮件来扩展它。如果用户单击确认 link,则 new_email
的值将覆盖 email
的值。因此,我希望它最初保存到 new_email
,只有在确认后才保存到 email
.
您可以使用 before_update
回调并将其值分配给 new_email
字段。
您可以为输入字段提供默认值:
<%= f.email_field :new_email, value: @user.email, class: 'form-control' %>