没有路由匹配[PATCH]“/ users / 1 / edit”

时间:2016-10-18 15:48:34

标签: ruby-on-rails routing railstutorial.org

我是rails的新手,目前在使用rails教程时遇到问题。我在示例应用程序的第10章。我检查确保资源:用户包含在routes.rb配置文件中,但我仍然想出这个错误 - 请帮助!

No route matches [PATCH] "/users/1/edit". 

运行rails路由产生

   Prefix Verb   URI Pattern               Controller#Action
     root GET    /                         static_pages#home
     help GET    /help(.:format)           static_pages#help
    about GET    /about(.:format)          static_pages#about
  contact GET    /contact(.:format)        static_pages#contact
   signup GET    /signup(.:format)         users#new
         POST   /signup(.:format)         users#create
    login GET    /login(.:format)          sessions#new
         POST   /login(.:format)          sessions#create
logout DELETE /logout(.:format)         sessions#destroy
    users GET    /users(.:format)          users#index
         POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
        PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
       DELETE /users/:id(.:format)      users#destroy

我的文件配置如下:

配置/ routes.rb中

Rails.application.routes.draw do
  root                   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/signup',  to: 'users#new'
  post   '/signup',  to: 'users#create'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users
end

模型/ user.rb

  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true

/users/_form.html.erb

<%= form_for(@user, url: yield(:url)) do |f| %>
  <%= render 'shared/error_messages', object: @user %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

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

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

  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

用户/ new.html.erb

<% provide(:title, 'Sign up') %>
<% provide(:button_text, 'Create my account') %>
<% provide(:url, signup_path) %>

<h1>Sign up</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= render 'form' %>
  </div>
</div>

用户/ edit.html.erb

<% provide(:title, 'Edit user') %>
<% provide(:button_text, 'Save changes') %>
<% provide(:url, edit_user_path(@user)) %>

<h1>Update your profile</h1>
<div class="row">
   <div class="col-md-6 col-md-offset-3">
    <%= render 'form' %>
     <div class="gravatar_edit">
      <%= gravatar_for @user %>
      <a href="http://gravatar.com/emails" target="_blank"    rel="noopener">Change</a>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:6)

你不应该做一个PATCH来编辑。你应该做一个PATCH来更新。

<% provide(:url, user_path(@user)) %>

按照惯例,您通常会在控制器上调用edit(您正在执行的编辑_user_path(@user)的GET方法。这会导致您的edit.html.erb被渲染,而这正在呈现表单。表单本身然后应该在users_path(@user)上通过PATCH方法调用控制器上的更新操作(而不是编辑操作)。该URL与POST(创建新用户时)相同,因此您可以删除该行

<% provide(:url, user_path(@user)) %>

并将form_for更改为仅使用users_path

<%= form_for(@user, url: user_path) do |f| %>

请注意,不要使用&#39; users_path&#39;因为链接到用户索引,你将得到一个错误,因为没有PATCH方法可用于该路由。

答案 1 :(得分:0)

您尝试访问编辑控制器操作的方式与您已经使用的路由模式不一致。试试这个:

Rails.application.routes.draw do
  root                   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/signup',  to: 'users#new'
  post   '/signup',  to: 'users#create'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  patch '/users/:id/edit', to: 'users#edit'
  resources :users
end

答案 2 :(得分:0)

@David的回答很好,但是以这种方式解决会破坏注册表单(及其相关测试)。 修复此模块的方法是将edit.html.erb中的提供更改为用户路径,如下所示:

更改自:

<% provide(:url, edit_user_path(@user)) %>

<% provide(:url, user_path(@user)) %>