Ruby - Destroy Method

From Logic Wiki
Jump to: navigation, search


Delete a record / Destroy Method

To add the destroy method, first add the following to the articles controller:

def destroy
  @article = Article.find(params[:id])
  @article.destroy
  flash[:notice] = "Article was successfully deleted"
  redirect_to articles_path
end

Then in the index.html.erb (listings page) add the following link as one of the <td> items under the show article link:

<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>