如何销毁关联本身并保留关联的对象,同时保持 RESTful?
具体来说,我有这些模型:
class Event < ActiveRecord::Base
has_many :model_surveys, :as => :surveyable, :dependent => :destroy, :include => :survey
has_many :surveys, :through => :model_surveys
end
class ModelSurvey < ActiveRecord::Base
belongs_to :survey
belongs_to :surveyable, :polymorphic => true
end
class Survey < ActiveRecord::Base
has_many :model_surveys
end
也就是说,该事件是:surveyable
(ModelSurvey belongs_to Event
)。我的问题是,无需创建ModelSurveysController
,我该如何销毁ModelSurvey
,同时离开Event
and Survey
alone?
一些与map.resources :events, :has_many => :model_surveys
?我不太确定在这种情况下该怎么办。路由需要做什么,控制器需要做什么?我希望网址看起来像这样:
/events/:title/model_surveys/:id
感谢您的帮助,
槊
在 Rails 2.3 中你有accepts_nested_attributes_for
这可以让您将 ModelSurvey 数组传递给相关事件。如果您允许通过嵌套属性声明进行销毁,您将能够通过event[model_surveys][1][_destroy]=1
并且该关联将被删除。查看api docs http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)