gpt4 book ai didi

javascript - Ruby on Rails 教程 destroy Action 结果展示

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:39 24 4
gpt4 key购买 nike

我正在按照教程进行操作,但在尝试删除/销毁模型中的记录时遇到了一个恼人的问题。销毁 Action 似乎导致表演 Action 。很多在线信息表明这是一个 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 to DELETE 的请求,从而使 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" %>

一些提示:

#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, "&nbsp;", 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,我只是发现使用它比使用 vanilla HTML 更好(确保支持任何 future 的代码)。

另外,尽可能地使用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 %>

关于javascript - Ruby on Rails 教程 destroy Action 结果展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com