没有路由匹配“/ contact1”

我是rails的新手,并且在Rails 4.2.3应用程序中遇到了上述错误。 我在更新资源时收到此错误:

No route matches [PATCH] "/contact.1" 

这是我的应用程序代码片段:

的routes.rb

 resources :contacts 

车型/ contact.rb

 class Contact < ActiveRecord::Base belongs_to :user validates :name, presence: true, length: { maximum: 50 } validates :number, presence:true end 

意见/联系人/ edit.html.erb

 

Editing Contact






控制器/ 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

我也试过添加url: contact_path(@contact), method: :patch to form_for但没有运气。

谁能告诉我代码有什么问题?

在您的控制器中,尝试将redirect_to从“redirect_to contacts_url”更改为“redirect_to contacts_path”。 在控制器内部,您应该使用路径而不是URL。 所以你的代码将是这样的:

  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