Ruby on rails 为什么';t接受\u嵌套的\u属性\u以允许\u销毁不工作?
我有一个嵌套的属性/资源,我正试图销毁它。我想做的是在相关的life_hack文章对象被销毁时销毁一个review对象。现在我不太确定接受的_as_嵌套的_属性是如何工作的,但我非常确定这是我需要的方法。以下是我定义的一些路线:Ruby on rails 为什么';t接受\u嵌套的\u属性\u以允许\u销毁不工作?,ruby-on-rails,activerecord,associations,Ruby On Rails,Activerecord,Associations,我有一个嵌套的属性/资源,我正试图销毁它。我想做的是在相关的life_hack文章对象被销毁时销毁一个review对象。现在我不太确定接受的_as_嵌套的_属性是如何工作的,但我非常确定这是我需要的方法。以下是我定义的一些路线: Hacklife::Application.routes.draw do devise_for :users root 'life_hacks#index' resources :life_hacks, only: [:new, :index, :crea
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文章模型:
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
class-LifeHack
我的审查模式:
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
class Review
现在我不太确定如何解决这个问题。通过反复试验,我使用了allow destroy接受的属性和常规的破坏方法,但没有任何用处。控制器文件中可能有错误,或者审查模型中没有指定错误?据我所知,如果你有一个嵌套的资源,你需要使用一个accepts\u nested\u attributes\u for:something\u destroy:true才能工作。至少这是ActiveRecordRails指南的声音。有什么想法吗?Thx.您可以使用
\u destroy
键销毁现有记录
根据本指南:
:允许销毁
如果为true,则使用_destroy从属性哈希中销毁任何成员
键和计算结果为true的值(例如1、'1',true或'true')。
默认情况下,此选项处于禁用状态
如果您能展示控制器的详细信息,我们可以更好地为您提供帮助。希望有帮助:)您使用的是什么版本的rails?你能连接你的lifehack控制器吗?嘿,谢谢Rajesh,很抱歉回复晚了。我回到我的问题,结果发现我的测试规范有问题。但是谢谢你的回复,我很感激。