Javascript RubyonRails教程在show中销毁操作结果
我正在学习教程,在尝试删除/销毁模型中的记录时遇到了一个恼人的问题。销毁操作似乎会导致显示操作。很多在线信息表明这是一个javascript问题,但application.js已经包含了//=requirejquery\u ujs和//=requirejquery。Javascript RubyonRails教程在show中销毁操作结果,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我正在学习教程,在尝试删除/销毁模型中的记录时遇到了一个恼人的问题。销毁操作似乎会导致显示操作。很多在线信息表明这是一个javascript问题,但application.js已经包含了//=requirejquery\u ujs和//=requirejquery。 () index.html.erb <h1>Hello, Rails!</h1> <%= link_to 'My Blog', controller: 'articles' %
() 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 %>
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
<!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>
你好,Rails!
上市文章
标题
正文
体裁
评级
articles\u controller.rb
<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 %>
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
<!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>
class-ArticlesController
application.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 %>
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
<!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来更改请求,从而使delete
方法起作用
destroy
链接不起作用的主要原因(您的链接路由到show
,这基本上意味着它使用的是GET
而不是DELETE
),是因为Rails没有正确地翻译您的方法
:
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
#app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
您是否检查了浏览器控制台?我99%确定这是一个javascript问题。我也是-看起来你的JS不会支持delete
方法这是控制台输出:GET 404(未找到)当我添加到索引文件时,我在刷新服务器时遇到一个typeError。ExecJS::ProgramError在文章中#索引并抱怨第6行,即“应用程序”在您的系统上安装NodeJS非常好!工作。非常感谢!我为什么要这么做?这是Rails标准安装的一部分吗?这与Rails如何处理javascript有关。本来应该有另一种方法来修复它,但我只是安装了Node,它就可以工作了ExecJS
错误几乎总是由此问题引起的^_^