[英]Ruby on Rails tutorial destroy action results in show
我正在阅读本教程,并尝试删除/销毁模型中的记录时遇到烦人的问题。 破坏动作似乎导致表演动作。 在线大量信息表明这是一个JavaScript问题,但是application.js已经包含// = require jquery_ujs和// = 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>
之前有这个问题...
-
基本上是因为您没有在应用程序/布局中包含JQuery UJS。
每当您单击它时,Rails都会分配一些JS将请求从GET
更改为DELETE
从而使delete
方法起作用。
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" %>
一些技巧:
#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 %>
您根本不需要使用content_tag
,我发现使用它比使用原始HTML更好(确保将来支持任何代码)。
另外,请尽可能使用loops
。 如此多的人不必要地重复代码:
<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.