【问题标题】:Issue with Destroy Method in Controller - ROR控制器中的销毁方法问题 - ROR
【发布时间】:2015-07-31 18:15:07
【问题描述】:

当我尝试从 wiki 中删除一篇文章时,它会抛出错误“找不到具有 id 的文章...”

销毁方法:

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

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

    redirect_to article_path
end

显示视图(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?"} %>

【问题讨论】:

  • 检查控制器中的参数
  • 也粘贴来自loginspecting params的参数
  • 错误信息已经很明显了。 ActiveRecord 只是抛出一个异常,它找不到任何与您的 params[:id] 匹配的记录。再次检查您的 params[:id],并确保您的数据库中有该记录
  • 这可能是错误 url @SomethVictory 的问题。例如主机/文章/文章/1。初学者面临这个问题
  • 我觉得删除应该没问题,url路径应该是/articles/:id。问题应该出在实例变量@article上,或者数据库中可能没有该记录,可能是他从控制台手动删除了它

标签: ruby-on-rails ruby-on-rails-4.1


【解决方案1】:

我遇到了同样的问题。您需要做的就是将“redirect_to article_path”更改为“redirect_to article_path”。它们是两条不同的路径。

祝你好运。

【讨论】:

    【解决方案2】:

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

    错误信息说明了一切。您正在尝试删除不存在的对象。它可能曾经存在,但它不再存在。

    示例:假设错误消息为"can't find article with id=5"

    打开你的rails console,搜索id=5的文章:

    Article.find(5)
    

    它会告诉你它找不到文章:

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

    您的代码看起来不错。只是您正在尝试删除不存在的内容。

    返回console,找到现有文章。尝试删除那篇文章,您会发现您的代码运行良好

    【讨论】:

      【解决方案3】:

      问题就出在这里:

      1. 您的数据库没有您要删除的记录
      2. 你没有通过正确的paramd[:id]

      没有看到你的参数日志,我只能参考你的link_to 方法,以便了解发生了什么。如果您想要更明智的回应,您需要向我们展示您收到的方法的 params 哈希

      --

      修复

      为什么不尝试直接调用对象(这样link_to 将能够尽可能有机地引用路径):

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

      当然,这是考虑到您的 @article 记录已正确编译等

      --

      您会遇到的另一个问题是您在销毁记录后调用redirect_to article_path

      您确定要重定向到articles_path - 文章的index(根据资源丰富的系统)?

      【讨论】:

        【解决方案4】:

        您的控制器代码是正确的...然后还要检查这两个方法...

        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
        

        检查您的视图 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>
        

        如果您遇到错误,那么 添加到您的 destroy 方法中

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

        【讨论】:

          【解决方案5】:

          您是否在articles_controller.rb 中插入代码?

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-12-18
            • 1970-01-01
            • 1970-01-01
            • 2015-07-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多