0

Been struggling through the rails guide build a blog tutorial. Have, I think, everything working aside from the destroy link on the index page. When clicked, it routes to the show view for that article that's clicked on to be deleted. this is from the index.html.erb. any help is greatly appreciated.

    def destroy 
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
end 

private
    def article_params
        params.require(:article).permit(:title, :text)
    end

1 Answers1

0

Code:

<%= link_to 'Destroy', articles_path(@article), method: :delete %>

The reason why the method option is required is because a browser cannot send a DELETE command necessary to perform the action. Providing the option sets an extra attribute and lets rails know the action you are about to perform should be routed to the deletion route instead of a simple show

Have a look at: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

Community
  • 1
  • 1
Jens
  • 1,132
  • 7
  • 14