mrateb
mrateb

Reputation: 2499

No route matches [POST] for update

I think the solution to this question should be obvious, but I tried out every suggested solution in all the similar questions but could not get my problem solved

I have a rails api project, that I needed to build a view for. I have a controller called password_resets_controller with functions edit and update.

My view for edit works fine, except that when the form is submitted, I get the following error:

No route matches [POST] "/password_resets/Ho5kqU9qvuc0LTiuotOYQw"

The reason this is very weird is that the request parameters are:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"WksU8TIWTJ2+WmqpKTiaaETWLD9hhFP1pzxQJAU73g51Cij6GTL0PZbLf8BuJI2l1HqHuNhOZwMs+qSVQxiPtQ==",
 "email"=>"[email protected]",
 "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Update password"}

which shows that the method is patch or put alright and not post

The relevant part in the view file is:

<%= form_for(@user, url: password_reset_path(params[:id]), method: :put) do |f| %>

  <%= hidden_field_tag :email, @user.email %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>

  <%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>

I know that the path I have is fine, as in routes I have:

resources :password_resets, only: [:create, :edit, :update]

And the routes that are dumped while posting show the is a correct path (this is a copy and paste from the table, so excuse the skewed result):

password_reset_path PATCH /password_resets/:id(.:format)
password_resets#update

PUT /password_resets/:id(.:format) password_resets#update

In the controller I have function alright.

def update 
end

I just want the code to reach this function but I'm stuck in the form submission. At first my form was without the :method. Adding it did not make the form work though. What could I be doing wrong?

Upvotes: 1

Views: 542

Answers (1)

ianrandmckenzie
ianrandmckenzie

Reputation: 482

In your code example, you have:

<%= form_for(@user, url: password_reset_path(params[:id]), method: :put) do |f| %>

Is the plurality of reset incorrect?

<%= form_for(@user, url: password_resets_path(params[:id]), method: :put) do |f| %>

Upvotes: 2

Related Questions