link_to'delete'不会破坏记录

问题描述 投票:0回答:1

我跟随Michael Hartl Chapter 13的轨道教程,但不是微博,我正在创造动物。

我对动物的看法显示了一个“删除”超链接,它应该从我的列表中删除一个动物记录,但是视图动作似乎没有达到它使用destroy方法的程度。 我在类似的帖子here中阅读了所有答案,但在我的案例中没有找到帮助的答案。

我正按照本教程中的说明,在AWS cloud9上的开发环境中。我真的很感激任何指针,因为我几天都在努力争取这个。

这是我的视图中的代码:

<li id="animal-<%= animal.id %>">
  <%= link_to gravatar_for(animal.user, size: 50), animal.user %>
  <span class="user"><%= link_to animal.user.name, animal.user %></span>
  <span class="ear_tag"><%= animal.ear_tag %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(animal.created_at) %> ago.
    <% if current_user?(animal.user) %>
      <%= link_to "delete", animal, method: :delete, data: { confirm: "You sure?" } %>
    <% end %>
  </span>
</li>

从Feed视图调用此视图:

<% if @feed_items.any? %>
  <ol class="animals">
    <%= render @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

来自主页视图:

<div class="col-md-8">
  <h3>Animal Feed</h3>
  <%= render 'shared/feed' %>
</div>

我已经多次回顾了<%= link_to...线,看起来是正确的。我的控制器代码是:

class AnimalsController < ApplicationController
  before_action :logged_in_user,  only: [:create, :destroy]
  before_action :correct_user,    only: :destroy

  def destroy
    @animal.destroy
    flash[:success] = "Animal deleted"
    redirect_to request.referrer || root_url
  end

  def correct_user
    @animal = current_user.animals.find_by(id: params[:id])
    redirect_to root_url if @animals.nil?
  end
end

我注意到我从来没有看到闪存“动物删除”,所以告诉我,我可能没有达到控制器方法中的那一点。

我的型号代码是:

class Animal < ApplicationRecord
  belongs_to :user
  default_scope -> {order(created_at: :desc) }
  validates :user_id, presence: true
  validates :ear_tag, presence: true, length: {maximum: 20}
end

这是app / assets / javascripts中的application.js文件:

//= require jquery
//= require bootstrap
//= require rails-ujs
//= require turbolinks
//= require_tree

这是我在浏览器中单击渲染视图中的“删除”标记后服务器日志所说的内容:

Started DELETE "/animals/308" for 23.25.133.17 at 2018-09-06 21:11:06 +0000
Processing by AnimalsController#destroy as HTML
  Parameters: {"authenticity_token"=>"vwF6cWow+6B3BxOiJElVY0aQmMGr4WLWDOCxgB0C03nRLcQDKC3YCUqBr4ahVwlSKN7bEYRrmGytyI1fPgvavw==", "id"=>"308"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 102], ["LIMIT", 1]]
  Animal Load (0.2ms)  SELECT  "animals".* FROM "animals" WHERE "animals"."user_id" = ? AND "animals"."id" = ? ORDER BY "animals"."created_at" DESC LIMIT ?  [["user_id", 102], ["id", 308], ["LIMIT", 1]]
Redirected to https://cabfa13dd4f34e67b634d4f52a7a046f.vfs.cloud9.us-west-2.amazonaws.com/
Filter chain halted as :correct_user rendered or redirected
Completed 302 Found in 4ms (ActiveRecord: 0.3ms)

我还发现我的一个测试失败了:

FAIL["test_animal_interface", AnimalsInterfaceTest, 1.5248641059999954]
 test_animal_interface#AnimalsInterfaceTest (1.53s)
        "Animal.count" didn't change by -1.
        Expected: 38
          Actual: 39
        test/integration/animals_interface_test.rb:33:in `block in <class:AnimalsInterfaceTest>'
ruby-on-rails ruby-on-rails-5
1个回答
6
投票

过滤器链暂停为:correct_user呈现或重定向

问题是你有@animals(没有定义)而不是@animal,所以redirect_to root_url if @animals.nil?总是成功,导致销毁行动总是失败。你应该将@animals改为@animal

def correct_user
  @animal = current_user.animals.find_by(id: params[:id])
  redirect_to root_url if @animal.nil?
end
© www.soinside.com 2019 - 2024. All rights reserved.