我将跟着Michael Hartl Chapter 13的rails教程一起学习,但是我不是在制作微博,而是在创作动物。
我的动物视图显示了一个“删除”超链接,该超链接应该从我的列表中删除动物记录,但是视图操作似乎根本无法使用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>
此视图是从供稿视图调用的:
<% 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>'
答案 0 :(得分: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