简体   繁体   English

控制器中销毁方法的问题-ROR

[英]Issue with Destroy Method in Controller - ROR

When I try to delete an article from a wiki, it throw an error "can't find article with id..." 当我尝试从Wiki删除文章时,它引发错误“找不到ID为...的文章”。

destroy method: 销毁方法:

def destroy
    @article=Article.find(params[:id])
    @article.destroy

    flash.notice="Article '#{@article.title}' was deleted"

    redirect_to article_path
end

show view (show.html.erb): 显示视图(show.html.erb):

<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<%= link_to "<<Back to Articles List", articles_path %>
<%= link_to "Delete", article_path(@article), method: :delete, data: {confirm: "Really delete this article?"} %>

When I try to delete an article from a wiki, it throw an error "can't find article with id..."

The error message says it all. 错误消息说明了一切。 You are trying to delete an object that does not exist. 您正在尝试删除一个不存在的对象。 It perhaps used to exist but it doesn't exist anymore. 它可能曾经存在,但现在不存在了。

example: let's say the error message says "can't find article with id=5" . 示例:假设错误消息显示"can't find article with id=5"

open your rails console , and search for article with id=5: 打开rails console ,然后搜索id = 5的文章:

Article.find(5)

it will tell you it could not find the article: 它会告诉您找不到文章:

ActiveRecord::RecordNotFound: Couldn't find Child with id=5

Your codes look good. 您的代码看起来不错。 it is just that you are trying delete something that does not exist. 只是您正在尝试删除不存在的内容。

back to the console , locate an existing article. 回到console ,找到现有文章。 try deleting that article and you'll notice your code is working fine 尝试删除该文章,您会发现您的代码运行正常

The problem is going to be here: 问题将在这里:

  1. Your db doesn't have the record you're trying to remove 您的数据库没有您要删除的记录
  2. You aren't passing the right paramd[:id] 您没有通过正确的paramd[:id]

Without seeing your params log, I can only reference your link_to method in order to gain ideas as to what happened. 在没有看到您的params日志的情况下,我只能引用您的link_to方法以获取关于发生的事情的想法。 If you want a more informed response, you'll want to show us the params hash you receive to your method 如果你想要一个更明智的回应,你会想向我们展示了params收到你的方法散列

-- -

Fix 固定

Why not try calling the object directly (so link_to will be able to reference the path as organically as possible): 为什么不尝试直接调用对象(因此link_to将能够尽可能link_to地引用路径):

<%= link_to "Delete", @article, method: :delete, data: {confirm: "Really delete this article?"} %>

This is, of course, considering your @article record was compiled correctly etc 当然,这是考虑到您的@article记录已正确编译等

-- -

Another issue you'll have is you are calling redirect_to article_path after you've destroyed your record. 您将遇到的另一个问题是,在销毁记录之后,您将调用redirect_to article_path

Surely you'll want to redirect to articles_path - the index of the articles (according to a resourceful system)? 您肯定要重定向到articles_pathindex (根据资源丰富的系统)吗?

Your Controller code is correct... then also check for both Methods... 您的控制器代码正确...然后还要检查这两种方法...

def index
    @article = Article.all

    respond_to do |format|
      format.html # call index.html.erb
      format.json { render json: @article }
    end
  end

def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to articles_url }
      format.json { head :no_content }
    end
  end

Check in your View article/index.html.erb file 签入您的View article / index.html.erb文件

<table>
  <tr>
    <th>Article Title</th>
    <th>Description</th>
    <th></th>
    <th></th>
    <th></th> 
  </tr>
<% @article.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.body %></td>
    <td><%= link_to 'Show', article %></td>
    <td><%= link_to 'Edit', edit_article_path(article) %></td>
    <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Article', new_article_path %>
</div>

Still if you are getting an error then add to your destroy method 如果仍然遇到错误,请添加到destroy方法中

@article = Article.find(params[:id]) rescue ""

I just get the same problem as this. 我只是遇到了同样的问题。 All you need to do is to change "redirect_to article_path" to "redirect_to articles_path". 您需要做的就是将“ redirect_to article_path”更改为“ redirect_to Articles_path”。 They are two different paths. 他们是两条不同的道路。

Good luck. 祝好运。

Dis you insert code at articles_controller.rb? 您是否在article_controller.rb中插入代码?

def show
    @article = Article.find(params[:id])
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM