路由错误没有路由匹配[DELETE]" / topics"

时间:2018-04-12 00:48:36

标签: ruby-on-rails controller routes

尝试让主题控制器的删除操作正常工作。  这是我的主题控制器

def destroy
@topic = Topic.find(params[:id])

if @topic.destroy
  flash[:notice] = "\"#{@topic.title}\" Topic was deleted!"
  redirect_to topics_path(@topic)
else
  flash.now[:alert] = "An error occurred. Please try again."
  render :show
end

我的路线:

 resources :topics do
    resources :bookmarks, except: [:index]
  end

我的索引视图上的删除链接:

<%= link_to "Delete Topic", @topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>

真的不明白我错过了什么。

1 个答案:

答案 0 :(得分:1)

尝试这些更改:

def destroy
  @topic = Topic.find(params[:id])

  if @topic.destroy
    flash[:notice] = "\"#{@topic.title}\" Topic was deleted!"
    redirect_to topics_path
  else
    flash.now[:alert] = "An error occurred. Please try again."
    render :show
  end
end

删除链接:

<%= link_to "Delete Topic", topic_path(@topic), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>