如何修复“无路由匹配[PATCH]” [英] how to fix "No route matches [PATCH]"

查看:138
本文介绍了如何修复“无路由匹配[PATCH]”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在轨道上的红宝石新手。我正在从事一个项目,遇到一个表单的问题。我正在使用设备进行身份验证。我有一个具有管理员和用户角色的用户类。设计生成的添加/更新方法为用户正常工作。当我尝试为管理员创建一个编辑页面时,我遇到无路线匹配[PATCH]错误。这是我使用的形式

 < h4>更新配置文件< / h4> 
<%= form_for @user,:url => {:controller => admin,:action => update} do | f | %GT;

<%= hidden_​​field_tag(:id,@ user.id)%>
< table>
< tr>
< td>名字< / td>
< td><%= f.text_field:first_name,:class => 形式控制 %>< / TD>
< / tr>
< tr>
< td>姓氏< / td>
< td><%= f.text_field:last_name,:class => 形式控制 %>< / TD>
< / tr>
< tr>
< td>电子邮件< / td>
< td><%= f.text_field:email,:class => 形式控制 %>< / TD>
< / tr>
< tr>
< td>< / td>
< td><%= f.submit更新,:class => btn btn-md btn-success pull-right%>< / td>
< / tr>
< / table>


<%end%>



这是我的控制器方法

  def edit 
end

def update
@user = User.find(params [:id])
如果request.post?
if(@ user.update_attributes(params [:first_name,:last_name,:email]))
redirect_to:action => admin_portal
else
render:action => edit
end
end

end

我也有路线

  get'admin / update'
get'admin / edit'

任何人都可以建议我如何解决这个问题。

GET ,而对于更新,您需要一个 PUT PATCH 方法。
当使用 PUT PATCH 时,有一些约定,但在您的情况下, c $ c> PATCH 路线会解决你的问题,如你所说

  patch'admin /:1 '

但是,显然你正在为每个REST方法写一条路线,而Rails有一个帮手结构称为资源,为您创建所有的REST方法。
您可以在 config / routes.rb 中创建一个入口,如:

 资源:admins 

它将生成旨在用于指向REST方法的每个路由对于您的 user_controller 并重命名为 admin 。只提供这行代码,相当于在你的配置/路由上写这些命令:

  get'admins'控制器:'admins',action::index 
get'admin /:id',controller:'admins',action::show
get'admin / new',controller:'admins' :$ new
get'admin /:id / edit',controller:'admins',action::edit
post'admin',controller:'admins',action::create
patch'admin /:id',controller:'admins',action::update
put'admin /:id',controller:'admins',action::update
delete'admin / ',controller:'admins',action::delete

您可以在 Rails指南。它有许多有用的建议创建路线。


I am a newbie to ruby on rails. I was working on a project and run into an issue with a form. I am using devise for authentication. I have a user class which has admin and user roles. The devise generated add/ update methods for user is working properly. I am running into a 'No route matches [PATCH]' error when I am trying to create an edit page for the admins. here is the form I am using

<h4>Update Profile</h4>
 <%= form_for @user, :url => {:controller => "admin", :action => "update" }  do |f|  %>

<%= hidden_field_tag(:id, @user.id) %>
<table>
  <tr>
    <td>First Name</td>
    <td><%= f.text_field  :first_name , :class => "form-control"%></td>
  </tr>
  <tr>
    <td>Last Name</td>
    <td><%= f.text_field  :last_name , :class => "form-control"%></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><%= f.text_field  :email , :class => "form-control"%></td>
  </tr>
  <tr>
    <td></td>
    <td><%= f.submit "Update", :class => "btn btn-md btn-success pull-right" %></td>
  </tr>
</table>

<%end%>

This is my controller method

def edit
end

def update
  @user = User.find(params[:id])
  if request.post?
     if(@user.update_attributes(params[:first_name, :last_name, :email] ))
      redirect_to :action =>  "admin_portal"
  else
    render :action => "edit"
  end
end

end

I also have the route

get 'admin/update'
 get 'admin/edit'

Can anyone suggest how I can fix this issue.

解决方案

The point is: you are setting only GET from HTTP's methods, and for updates, you need a PUT or a PATCH method. There are some conventions when to use PUT or PATCH, but in your case, making a PATCH route would solve your problem as you said

patch 'admin/:1'

But, apparently you are writing yourself a route for every REST method, and Rails has a "helper" structure called resources that create all the REST methods for your. You could create just one entrance on your config/routes.rb like:

resources :admins

and it would generate every route intended for the REST methods pointing for your user_controller and renamed as admin. Putting only that line of code, is be equivalent to write all these commands on your config/routes:

get 'admins', controller: 'admins', action: :index
get 'admin/:id', controller: 'admins', action: :show
get 'admin/new', controller: 'admins', action: :new
get 'admin/:id/edit', controller: 'admins', action: :edit
post 'admin', controller: 'admins', action: :create
patch 'admin/:id', controller: 'admins', action: :update
put 'admin/:id', controller: 'admins', action: :update
delete 'admin/:id', controller: 'admins', action: :delete

You can see more on Rails guides. It has many useful advices on creating routes.

这篇关于如何修复“无路由匹配[PATCH]”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆