When editing a basic User model in Rails from the edit.html.erb form, I would like to apply special logic if the User’s email address is changed. I am having trouble trying to setup the IF statement in the controller.
Here is what I have in the Users Controller:
class UsersController < ApplicationController
.
.
.
def edit
@user = User.find(params[:id])
end
def update
current_email = @user.email
new_email = params[:user][:email]
if new_email = current_email
#update user as usual
else
#send email to new email address to confirm
end
end
.
.
end
When debugging, I find that lines 1 and 2 of the Update action are working correctly. Specifically, when I change the email on the edit User form, the variables are set correctly. But the IF statement (line 3 in the Update action) always seems to return true.
Here is the edit.html.erb form that I am using:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %><br />
<%= f.label :last_name %><br />
<%= f.text_field :last_name %><br />
<%= f.label :email %><br />
<%= f.text_field :email %><br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %><br />
<%= f.submit "Update" %>
Any help with this will be greatly appreciated.
The evaluation operator is == not =. It should read: