I have the following form and controller.
<%= form_tag do %>
<div>
<%= label_tag :Old %>
<%= password_field_tag :old_password %>
</div>
<div>
<%= label_tag :New %>
<%= password_field_tag :new_password %>
</div>
<div>
<%= label_tag :Confirm %>
<%= password_field_tag :confirm_password %>
</div>
<div>
<%= submit_tag "Update" %>
</div>
<% end %>
and the controller:
def change
@user = current_user
@op = params[:old_password]
@np = params[:new_password]
@cp = params[:confirm_password]
if @np == @cp
@user.update_with_password(:password => @np, :current_password=>@op)
if @user.save
flash[:notice] = "Password Successfully Changed"
redirect_to home_path
end
else
@user.errors.add("Incorrect confirmation")
end
end
This is all tied to ‘password/change’ in the config/routes.rb
The problem is that when I go to /password/change I immediately an redirected to home and receive the “password successfully changed” flash notice. What I take from it is that it is not requiring me to click the submit button in order to pass the parameters. How do I make it so that it waits for the submission of the form before continuing through the controller?
The problem is that you need 2 separate methods. One to show the view and one to handle the form post.
Right now the “change” method in your password_controller is handling the form post so you need a method like “index” to show the form
Then in your markup add the action to your form
Then in your controller you can specify POST only for the change method
UPDATE
Instead of using the verify method (removed in rails 3) you should either set up your route to be restful (Rails Routing from the Outside In) or you can create a specific route for changing the password like so: