- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ruby 1.8.7 和 Rails 3.0.3 阅读 PragProg 的 Agile Rails 开发手册
我的 ProductsController 上的这个销毁方法不会删除我的产品,我不明白为什么。
这是我的第一个剪辑,我希望“能正常工作”
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
但是我断言 Product.count 的测试失败了。
如果我更改为使用这样的删除类方法:
def destroy
Product.delete(params[:id])
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
我的测试通过了。
这是测试
test "should destroy product" do
assert_difference('Product.count', -1) do
if @product.referenced_by_line_item
@product.line_items.remove_all
end
delete :destroy, :id => @product.to_param
end
assert_redirected_to products_path
end
我的产品模型类有一个 before_destroy 方法
def referenced_by_line_item
if line_items.count.zero?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
你知道我在这里做错了什么吗?通过阅读文档(并在此处搜索其他问题),我希望 @product.destroy 和 Product.delete(id) 做同样的事情。
谢谢!
最佳答案
您在执行任何操作之前都会调用referenced_by_line_item。如果存在订单项,则返回 false,因此您不会删除订单项。我认为这不是您想要的行为。如果您将其更改为:
if <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="50711020223f342533247e2235363522353e333534" rel="noreferrer noopener nofollow">[email protected]</a>_by_line_item
@product.line_items.remove_all
end
delete :destroy, :id => @product.to_param
那么@product.destroy
可能会起作用。
尽管颠倒 referenced_by_line_item
中的逻辑可能更有意义。更有意义的说法是,如果存在与该产品关联的订单项,则该产品由订单项引用。我认为你的逻辑是倒退的。
此外, bool 方法应以“?”结尾
不过我看到了这里的复杂性。你希望它在有行项的时候返回false,这样就不保存,但是返回值与方法名不一致。也许这样的东西会更好:
def no_line_items_present?
if line_items.count > 0
# add errors
return false
else
return true
end
end
然后你可以说:
@product.line_items.remove_all unless @product.no_line_items_present?
delete :destroy, :id => @product.to_param
尽管我不认为在决定删除订单项之前检查订单项有什么意义,除非性能略有提高。
关于ruby-on-rails - destroy 方法似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461956/
我有两个类(class): parent 和 child child : belongs_to :parent 和 家长 has_many :children, :dependent => :dest
我有这个模型 class XmlImport ["text/xml"] end :event_import_records 条目正在被销毁。但是 :events 不是。 has_many throu
我定义了以下简单的管道: image: name: hashicorp/terraform:light entrypoint: - '/usr/bin/env' - 'PATH
我定义了以下简单的管道: image: name: hashicorp/terraform:light entrypoint: - '/usr/bin/env' - 'PATH
在我们的应用程序中,我们覆盖了 ActiveRecord destroy 方法,以便我们的记录不会被删除(因此用户可以取消删除)。像这样: def destroy self.is_deleted
我有一个笔记模型,具有以下关联 注意.rb has_many :note_categories, :dependent => :destroy has_many :categories, :throu
我是一名 Python 初学者。尝试制作一个新按钮来关闭窗口。我收到错误消息: Exception in Tkinter callback Traceback (most recent call la
我在我的应用程序中添加了评论功能,到目前为止一切正常,直到出现此错误。我所做的是添加了删除评论功能。当我启动服务器时一切正常,但在我单击“删除”按钮后问题出现了。 错误信息 undefined loc
Rails,def destroy,没有响应 destroy.js.erb 这是我的方法: # DELETE /Groups/1 # DELETE /Groups/1.xml def de
假设我有一个 MyClass 对象的集合 MyCollection。 Set MyCollection = Nothing 是否调用每个包含的对象的析构函数,或者我应该单独设置每个对象 = Nothi
我正在尝试使用 Node.js、Express 和 Mongodb 构建 REST API。我正在使用 mongodb npm 包连接到数据库,下面是我的 sever.js 文件代码 var expr
我有一个小问题,我有以下两个模型: class CriticalProcess :destroy has_many :roles, :through => :authorizations a
我正在使用 ng2-toastr 并收到以下错误 https://www.npmjs.com/package/ng2-toastr Attempt to use a destroyed view: d
基本信息: 系统: # cat /proc/version Linux version 3.10.0-514.2.2.el7.x86_64 (builder@kbuilder.dev.centos.o
一 点睛 destroy 用于销毁 ThreadGroup,该方法只是针对一个没有任何 active 线程的 group 进行一次 destroy 标记,调用该方法的直接结果是在父 group 中将自
我开始注意到 Scope::Guard 的一些奇怪之处。 . 如果我取消定义 $guard变量作为 sub 中的最后一个语句,守卫的 sub 得到 比我预期的要晚打电话。 如果我不取消它,或者如果我做
我正在构建一个提供打开和关闭它的服务的模式。该模式有一个控制关闭按钮的小 Controller ,以及进入模式内容的模板的 $compile。 那个模板就是一个组件,当然,那个组件有一个 Contro
一直在文件馆和网上寻找答案,但没有真正找到答案,只是零零碎碎。似乎有很多建议的帖子,但没有一个有答案。 我有一个使用范围的复杂指令:true。它实际上是我正在尝试为其编写清理代码的 ng-grid 最
给定以下 Delphi 代码,Foo 在 FormClose 上是 Free,但 TFoo.Destroy 是没有被调用 - 因此 Bar 没有被 Free'd,导致内存泄漏? 我是否在这里错过了一些
https://docs.angularjs.org/guide/directive By listening to this event, you can remove event listener
我是一名优秀的程序员,十分优秀!