Reputation: 61
I'm following the tutorial and having an annoying problem when trying to delete/destroy a record in my model. The destroy action seems to result in a show action. A lot of the information online suggests it's a javascript problem but application.js already contains //= require jquery_ujs and //= require jquery.
(http://guides.rubyonrails.org/getting_started.html)
index.html.erb
<h1>Hello, Rails!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Genre</th>
<th>Ratings</th>
<th colspan="3"></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article.genre %></td>
<td><%= article.ratings %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', articles_path %>
articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if
@article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
end
private
def article_params
params.require(:article).permit(:title, :text, :genre, :ratings)
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag :default, 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Upvotes: 2
Views: 949
Reputation: 76774
Had this problem before...
--
It's basically because you've not included JQuery UJS in your application / layout.
Rails makes the delete
method work by assigning some JS to change the request from GET
to DELETE
whenever you click it.
The main reason why destroy
links don't work (yours is routing to show
, which basically means it's using GET
rather than DELETE
) is because Rails is not translating your method
properly:
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
#app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
Some tips:
#app/views/articles/index.html.erb
<%= content_tag :h1, "Hello, Rails!" %>
<%= link_to 'My Blog', articles_path %>
<%= content_tag :h1, "Listing articles" %>
<%= link_to 'New article', new_article_path %>
<% attrs = %i(title text genre ratings) %>
<%= content_tag :table do %>
<%= content_tag :tr do %>
<% attrs.each do |attr| %>
<%= content_tag :th, attr.to_s.titleize %>
<% end %>
<%= content_tag :th, " ", colspan: 3 %>
<% end %>
<% end %>
<% @articles.each do |article| %>
<%= content_tag :tr do %>
<% attrs.each do |attr| %>
<%= content_tag :td, article.send(attr) %>
<% end %>
<% end %>
<%= content_tag :td, link_to('Show', article) %>
<%= content_tag :td, link_to('Edit', edit_article_path(article)) %>
<%= content_tag :td, link_to('Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
<%= link_to 'Back', articles_path %>
You don't have to use content_tag
at all, I just find it better to use it than vanilla HTML (makes sure any future code is supported).
Also, try your best to use loops
wherever you can. So many people repeat code needlessly:
<td><%= @article.title %></td>
<td><%= @article.text %></td>
<td><%= @article.date %></td>
<td><%= @article.news %></td>
...
<% attrs = %i(title text date news) %>
<% attrs.each do |a| %>
<td><%= @article.send a %></td>
<% end %>
Upvotes: 3