路由错误没有路由匹配(正确的路由)

Routing Error No Route Matches (correct route)

我正在构建一个编辑表单。我已经完成了表格,它按应有的方式呈现。当我将更新提交到表单时,出现无路由错误。我的编辑页面的路径例如是“/topics/1/bookmarks/1/edit”。此页面加载完美。该页面包含将用于编辑记录的部分表单。但是,当我 select 提交按钮时,它会重新路由到“/topics/1/bookmarks/1”并给我以下内容:

Routing Error
No route matches [PATCH] "/topics/1/bookmarks/1"

以下是应该很重要的文件,如果有我没有分享的内容,请告诉我。这很重要。

bookmarks_controller.rb

def edit
  @topic = Topic.find(params[:topic_id])
  @bookmark = Bookmark.find(params[:id])
end

def update
  @topic = Topic.find(params[:topic_id])
  @bookmark = Bookmark.find(params[:id])

  if @bookmark.update_attributes(params.require(:bookmark).permit(:url, :topic_id, :description))
    flash[:notice] = "Bookmark was updated"
    redirect_to [@topic, @bookmark]
  else
    flash[:error] = "There was an error saving the Bookmark.  Please try again."
    render :edit
  end
end

config/routes.rb

resources :topics do
  resources :bookmarks, only: [:show, :new, :edit]
end

bookmarks/_form.html.erb

<%= form_for [topic, bookmark] do |f|  %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.label :url %>
  <%= f.text_field :url %>
  <%= f.submit %>
<% end %>

bookmarks/edit.html.erb

 <%= render partial: 'form', locals: {topic: @topic, bookmark: @bookmark} %>

您没有更新路由,这是实际更新数据库的途径。只是改变

 resources :bookmarks, only: [:show, :new, :edit] 

resources :bookmarks, only: [:show, :new, :edit, :update]

或者更好的是,

resources :bookmarks, except: [:index, :create, :destroy] 

如果您有一个新动作,那么您应该也需要一个创建动作。所以,最后:

resources :bookmarks, except: [:index, :destroy]