Validation errors on model available in the controller but not in the view - help please?
I've got a password reset form setup that is not displaying any errors at all and it's killing me. If I input a password (but no confirmation) -- I expect to see 2 errors.
Those 2 errors are in the controller (I can print them out). But they're not showing up in the view...help! I'm definitely calling a render rather than redirect but cannot see what I'm doing wrong :S.
It's also not working when I manually add an error as in the first if statement :(
Code is:
before_action :get_user, only: [:edit, :update]
def edit
end
def update
if params[:user][:password].empty?
@user.errors.add(:password, "can't be empty")
puts "Error count for user = #{@user.errors.count}"
render 'edit'
elsif @user.update(user_params)
log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
puts "Error count for user = #{@user.errors.count}"
render 'edit'
end
end
-- View
<% provide(:title, 'Reset password') %>
Reset password
<%= "Error count for user = #{@user.errors.count}" %>
UPDATE
Checked that the object was the same in controller and view. On first load – the @user.object_id.to_s variable is identical in the controller and view.
When re-rendered, the object_id in the view is the same as previously. The object_id in the controller is changing. So the controller object has the errors, the model object in the view does not (it's not updating).
...continuing to search through the code!