没有路由匹配 [PATCH] "/contact.1"; [英] No route matches [PATCH] "/contact.1"
问题描述
我是 Rails 新手,正在努力解决 Rails 4.2.3 应用程序中提到的错误.更新资源时出现此错误:
I am new to rails and struggling with the mentioned error in Rails 4.2.3 application. I am getting this error while updating a resource:
No route matches [PATCH] "/contact.1"
这是我的应用程序代码片段:
Here is my application code snippets:
routes.rb
resources :contacts
models/contact.rb
class Contact < ActiveRecord::Base
belongs_to :user
validates :name, presence: true, length: { maximum: 50 }
validates :number, presence:true
end
views/contacts/edit.html.erb
<h1> Editing Contact </h1>
<%= form_for @contact do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<%= f.label :number %><br>
<%= f.text_field :number%><br><br>
<%= f.submit "Update Contact", class: "btn btn-primary" %>
<% end %>
controllers/contacts_controller.rb
def edit
@contact = Contact.find(params[:id])
end
def update
@contact = Contact.find(params[:id])
if @contact.update(contact_params)
flash[:success] = "Contact Update Successfully..."
redirect_to contacts_url
else
render 'edit'
end
end
令人惊讶的是,路径生成为 /contact.1
而不是 /contact/1
.
Surprisingly the path being generated as /contact.1
instead of /contact/1
.
我也尝试将 url: contact_path(@contact), method: :patch
添加到 form_for
但没有运气.
I also tried by adding url: contact_path(@contact), method: :patch
to form_for
but no luck.
谁能告诉我代码有什么问题?
Can anybody tell me what is wrong with the code?
推荐答案
在您的控制器中,尝试将您的 redirect_to 从redirect_to contacts_url"更改为redirect_to contacts_path".在您的控制器中,您应该使用路径而不是 url.所以你的代码会是这样的:
In your controller, try changing your redirect_to from "redirect_to contacts_url" to "redirect_to contacts_path". Inside your controller you should use path instead of url. So your code will be like this:
def update
@contact = Contact.find(params[:id])
if @contact.update(contact_params)
flash[:success] = "Contact Update Successfully..."
redirect_to contacts_path
else
render 'edit'
end
end
这篇关于没有路由匹配 [PATCH] "/contact.1";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!