I think this question might have been asked before, but I honestly don’t know how to search for it.
Basically, when I do a render :action => ‘edit’ in the update action in controller, somehow the view outputs the form as if it’s a :action => ‘new’ page.
form_for gave the wrong action and f.submit gave wrong button text (it gave create instead of update)
edit:
relevant parts of controller
def edit
@user = User.find_by_email(current_user.email)
end
def update
old_password=params[:user].delete(:old_password)
@user=User.new(params[:user])
if User.find_by_email(@user.email).valid_password?(old_password)
logger.info 'Valid old password'
else
flash[:notice]='Invalid current password'
render :action=>'edit'
end
end
As discussed in the comments, @bassneck is right – while you are rendering the edit view, the
form_forcall looks at whether the object is persisted or not (@user.persisted?). This has the benefit in a lot of cases of being able to use one piece of form code for both new and edit views (I’ll generally have a partial_form.html.erbthat gets used for both situations).In your case though, it isn’t leading to the desired behaviour – so wwhat you need to do is make sure you’re using the relevant user object. If you want to update a user,
@usershould be the object you want to update.