[英]Ruby on Rails tutorial destroy action results in show
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.
在线大量信息表明这是一个JavaScript问题,但是application.js已经包含// = require jquery_ujs和// = require jquery。
( http://guides.rubyonrails.org/getting_started.html ) ( http://guides.rubyonrails.org/getting_started.html )
index.html.erb 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 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 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>
Had this problem before... 之前有这个问题...
-- -
It's basically because you've not included JQuery UJS in your application / layout. 基本上是因为您没有在应用程序/布局中包含JQuery UJS。
Rails makes the delete
method work by assigning some JS to change the request from GET
to DELETE
whenever you click it. 每当您单击它时,Rails都会分配一些JS将请求从
GET
更改为DELETE
从而使delete
方法起作用。
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: destroy
链接不起作用的主要原因(您正在路由到show
,这基本上意味着它正在使用GET
而不是DELETE
)是因为Rails没有正确转换您的method
:
#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). 您根本不需要使用
content_tag
,我发现使用它比使用原始HTML更好(确保将来支持任何代码)。
Also, try your best to use loops
wherever you can. 另外,请尽可能使用
loops
。 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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.