为什么不accepts_nested_attributes_for allow_destroy不工作? [英] Why doesn't accepts_nested_attributes_for allow_destroy not work?

查看:160
本文介绍了为什么不accepts_nested_attributes_for allow_destroy不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的属性/资源,我正在试图摧毁。我想要做的是摧毁审查对象,当相关life_hack文章对象被销毁。现在,我也不太清楚了accepted_as_nested_attributes是如何工作的,但我是pretty的肯定,这是我需要做的方式。下面我有一些路线定义为这样:

I have a nested attribute/resource, that I'm trying to destroy. What I want to do is to destroy a review object, when an associated life_hack article object is destroyed. Now I'm not too sure how the accepted_as_nested_attributes work but I'm pretty sure this is the way that I need to do it. Below i have some routes defined as so:

Hacklife::Application.routes.draw do
  devise_for :users
  root 'life_hacks#index'

  resources :life_hacks, only: [:new, :index, :create, :show, :destroy] do
    resources :reviews, only: [:new, :create]
  end

  resources :reviews, only: [:show] do
    resources :comments, only: [:new, :create]
  end

下面是我的LifeHack文章模型:

Below is my LifeHack article model:

class LifeHack < ActiveRecord::Base
 validates :title, presence: true
 validates :content, presence: true

 belongs_to :user
 has_many :reviews, dependent: :destroy
 accepts_nested_attributes_for :reviews, allow_destroy: true
end

我的审查模式:

My Review Model:

class Review < ActiveRecord::Base
validates :title, presence: true
validates :body, presence: true
validates :rating, presence: true, numericality: { only_integer: true, greater_than: 0,     
less_than_or_equal_to: 6 }

has_many :comments
belongs_to :user
belongs_to :life_hack
end

现在我不太知道如何解决这个问题。通过审判错误我用两个accepted_as_attribute_for允许破坏和普通的has_many destory方法,但没有用。难道还有错的财产以后或许在控制器文件,或者也许有一个没有被在审查模式指定的东西吗?从我的理解,如果你有一个嵌套的资源,你需要使用一个accepts_nested_attributes_for:东西allow_destroy:如此才能为它工作。至少这就是ActiveRecord的导轨导向使得它的声音。有什么想法吗? THX。

Now I'm not quite sure how to fix this. Through trial by error I've used both the accepted_as_attribute_for allow destroy and the regular has_many destory methods but to no use. Could there be somthing wrong perhaps in a controller file, or maybe there is something that is not being specified in the review model? From what I understand, if you have a nested resource, you need to use an accepts_nested_attributes_for :something allow_destroy: true in order for it to work. At least that's what the activerecord rails guide made it sound. Any thoughts? Thx.

推荐答案

您可以使用 _destroy 键破坏现有的记录。

You can use the _destroy key to destroy existing records.

根据指南此参考:

As per this reference guide:

:allow_destroy

:allow_destroy

如果属实,破坏从属性的所有成员凑了_destroy   键和值计算结果为真(如:1,'1',真的,或者true)。   此选项默认是关闭的。

If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (eg. 1, '1', true, or 'true'). This option is off by default.

如果你可以只显示控制器的详细信息,我们可以更好的为您提供帮助。希望它可以帮助:)

If you could just show the controller details, we could better assist you. Hope it helps :)

这篇关于为什么不accepts_nested_attributes_for allow_destroy不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆