尝试让主题控制器的删除操作正常工作。 这是我的主题控制器
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?' } %>
真的不明白我错过了什么。
答案 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?' } %>