Ruby-on-rails 更新记录 rails 4 没有路由匹配 [PATCH] "/admin/usersupdate"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20535025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:08:09  来源:igfitidea点击:

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

ruby-on-railsrubyruby-on-rails-4

提问by Kalvin Manson

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

而这个表单在 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.

请帮忙。

回答by Daniel Arenas

change the method in the form method: :post

更改表单方法中的方法::post

your header of form

你的表格标题

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

回答by skplunkerin

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

我遇到了同样的问题,您需要正确更新 routes.rb 文件。

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

我猜你的路由文件中定义此路径的行以“post”开头,请尝试将其更改为“patch”。

Inside of config/routes.rb

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.

在我做出这个改变后,一切对我来说都很顺利。

回答by xdazz

You defined the route with POST, but you use PATCHin the form.

您使用 定义了路线POST,但您PATCH在表单中使用。