Jas*_*123 0 ruby-on-rails ruby-on-rails-5
我跟随迈克尔·哈特尔(Michael Hartl)第13章的导轨教程,但不是微博,我正在创造动物.
我对动物的看法显示了一个"删除"超链接,该超链接应该从我的列表中删除动物记录,但是视图动作似乎没有达到它使用destroy方法的程度.
我在这里阅读了类似帖子中的所有答案,但在我的案例中找不到那些答案.
我正按照本教程中的说明,在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>
Run Code Online (Sandbox Code Playgroud)
从Feed视图调用此视图:
<% if @feed_items.any? %>
<ol class="animals">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
来自主页视图:
<div class="col-md-8">
<h3>Animal Feed</h3>
<%= render 'shared/feed' %>
</div>
Run Code Online (Sandbox Code Playgroud)
我已多次审查该<%= 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
Run Code Online (Sandbox Code Playgroud)
我注意到我从来没有看到闪存"动物删除"所以告诉我,我可能没有达到控制器方法中的那一点.
我的型号代码是:
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
Run Code Online (Sandbox Code Playgroud)
这是app/assets/javascripts中的application.js文件:
//= require jquery
//= require bootstrap
//= require rails-ujs
//= require turbolinks
//= require_tree
Run Code Online (Sandbox Code Playgroud)
这是我在浏览器中单击渲染视图中的"删除"标记后服务器日志所说的内容:
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)
Run Code Online (Sandbox Code Playgroud)
我还发现我的一个测试失败了:
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>'
Run Code Online (Sandbox Code Playgroud)
过滤器链暂停为: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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |