Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating record rails 4 No route matches [PATCH] "/admin/usersupdate"

I have this controller

 def usersedit
   @user = User.find_by id: params[:id]
 end

 def usersupdate
   @user = User.find_by id: params[:id]
   if @user.update(post_params)
     redirect_to action: :users
   else
     render 'usersedit'
   end
 end

And this form in the view in usersedit.html.erb

<%= form_for :user, url: {action: "usersupdate"}, method: :patch do |f| %>

but when I submit the form this error appears

No route matches [PATCH] "/admin/usersupdate"

My routes code is:

     Prefix Verb URI Pattern                     Controller#Action
             root GET  /                               pages#home
      admin_index GET  /admin/index(.:format)          admin#index
     admin_grades GET  /admin/grades(.:format)         admin#grades
       pages_home GET  /pages/home(.:format)           pages#home
   pages_register GET  /pages/register(.:format)       pages#register
     pages_create POST /pages/create(.:format)         pages#create
      pages_login GET  /pages/login(.:format)          pages#login
    pages_logging POST /pages/logging(.:format)        pages#logging
     pages_logout GET  /pages/logout(.:format)         pages#logout
                  GET  /activate/:user/:hash(.:format) pages#activate
         remember POST /remember(.:format)             pages#remember
     reactivation POST /reactivation(.:format)         pages#reactivation
  admin_documents GET  /admin/documents(.:format)      admin#documents
      admin_users GET  /admin/users(.:format)          admin#users
                  GET  /admin/usersedit/:id(.:format)  admin#usersedit
admin_usersupdate POST /admin/usersupdate(.:format)    admin#usersupdate

Help please.

like image 700
Kalvin Manson Avatar asked Nov 27 '22 01:11

Kalvin Manson


2 Answers

change the method in the form method: :post

your header of form

form_for :user, url: {action: "usersupdate"}, :method => :POST do |f|
like image 161
Daniel Arenas Avatar answered Dec 19 '22 09:12

Daniel Arenas


I had the same problem, you'll need to update your routes.rb file correctly.

I'm guessing the line in your routes file where you've defined this path starts with "post", try changing that to "patch".

Inside of config/routes.rb

change:

post "admin/usersupdate" => "admin#usersupdate", :as => "admin/usersupdate"

to:

patch "admin/usersupdate" => "admin#usersupdate", :as => "admin/usersupdate"

Everything works swimmingly for me after I made this change.

like image 20
skplunkerin Avatar answered Dec 19 '22 08:12

skplunkerin