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

时间:2015-01-24 20:29:18

标签: ruby-on-rails routes nested-routes

我正在构建编辑表单。我已经完成了表格,并按照应有的方式呈现。当我将更新提交到表单时,我得到一个无路由错误。我的编辑页面的路径例如是' / topics / 1 / bookmarks / 1 / edit'。此页面加载完全正常。该页面包含将用于编辑记录的部分表单。当我选择提交按钮时,它会重新路由到' / 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

配置/ routes.rb中

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

书签/ _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 %>

书签/ edit.html.erb

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

1 个答案:

答案 0 :(得分:1)

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

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

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

或者更好,

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

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

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