【问题标题】:Ruby on Rails tutorial destroy action results in showRuby on Rails 教程中的销毁操作结果显示
【发布时间】:2015-10-25 10:53:56
【问题描述】:

我正在按照教程操作,但在尝试删除/销毁模型中的记录时遇到了一个烦人的问题。破坏动作似乎导致了表演动作。网上的很多信息表明这是一个 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>

【问题讨论】:

  • 你检查浏览器控制台了吗?我 99% 确定这是一个 javascript 问题。
  • 我也是——看来你的 JS 不会出现以支持 delete 方法
  • 这是控制台输出:GET localhost:3000/javascripts/default.js 404 (Not Found)

标签: javascript ruby-on-rails ruby


【解决方案1】:

之前遇到过这个问题...

--

这基本上是因为您没有在应用程序/布局中包含 JQuery UJS。

Rails 通过分配一些 JS 来更改 GET to 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, "&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,我只是发现它比普通 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 %>

【讨论】:

  • 当我将 添加到我的索引文件时,我在刷新服务器时收到 typeError。
  • ExecJS::ProgramError in Articles#index 并抱怨第 6 行,即“应用程序”行
  • 在你的系统上安装 NodeJS
  • 太棒了!工作。非常感谢!为什么我需要这样做?这是 Rails 标准安装的一部分吗?
  • 这与 Rails 处理 javascript 的方式有关。应该有另一种方法来修复它,但我只安装了 Node 就可以了。 ExecJS 错误几乎都是由这个问题引起的^_^
猜你喜欢
  • 2012-09-26
  • 2012-03-28
  • 1970-01-01
  • 2015-06-13
  • 2022-06-13
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
相关资源
最近更新 更多