我正在构建一个编辑表单。我已填妥表格,并按应有的方式呈现。当我向表单提交更新时,我会得到一个无路由错误。例如,到我的编辑页面的路径是‘/主题/1/书签/1/编辑’。这一页加载得非常好。该页包含将用于编辑记录的窗体的一部分。然而,当我选择submit按钮时,它会重新路由到‘/ the /1/bookmark/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
endconfig/scripes.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 %>书签/ed.html.erb
<%= render partial: 'form', locals: {topic: @topic, bookmark: @bookmark} %>发布于 2015-01-24 20:36:54
您没有更新路径,这才是真正更新数据库的方法。只要改变
resources :bookmarks, only: [:show, :new, :edit] 至
resources :bookmarks, only: [:show, :new, :edit, :update]或者更好,
resources :bookmarks, except: [:index, :create, :destroy] 如果您有一个新的操作,那么您也应该希望创建一个操作。因此,最后:
resources :bookmarks, except: [:index, :destroy]https://stackoverflow.com/questions/28129926
复制相似问题