Reputation: 42863
I am using rails and devise. It seams the only way to update a user object is to go through the form. Even when I use console or save(false) the devise objects doesn't seem to be working.
I don't want to display the new and confirm password unless a user clicks a link that says change password. This then changes a boolean on the user to true, and redirects to the user page and then an if/else/end shows the appropiate code.
The problem is that the first change_password does get set, but the second will not change it back. Crazy.
model
devise :registerable, :database_authenticatable, :recoverable,
:rememberable, :trackable, :validatable, :confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :name, :change_password
has_many :analytics
has_many :answers
before_save :change_password_to_false
def change_password_to_false
self.change_password = false
end
controller
def change_password
player = Player.find(current_player.id)
player.change_password = true
player.save(false)
redirect_to edit_player_registration_path
end
def keep_password_the_same
player = Player.find(current_player.id)
player.change_password = false
player.save(false)
redirect_to edit_player_registration_path
end
view
<% if current_player.change_password? %>
<h4><%= link_to "Keep Password The Same", keep_password_the_same_path %></h4>
<%= render 'change_password', :f => f %>
<% else %>
<h4><%= link_to "Change Password", change_password_path %></h4>
<% end %>
There were a few problems with the code but codevoice was able to get me in the right direction. I thought save(false) was bypassing callbacks too but that was not the case. So every time the user was updated even when the user was updated with save(false) it change_password
was getting set to false.
The second thing was giving the user the ability to remove the change password fields if they decided they didn't want to change it. So in the callback I just check if the change_password
field was just changed to true and then I don't do anything. If the password was already true and the user was just updated then you can reset it to false.
Upvotes: 0
Views: 519
Reputation: 474
in my opinion your change_pasword
attribute will be alweys false
beacasue you use before_save :change_password_to_false
. Thats mean that you are changeing change_password
to false
every time you save the Player
model
Upvotes: 1