RESTful 破坏 Rails 中的多态关联?
如何销毁关联本身并保留关联的对象,同时保持 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 isn't_to Event
)。我的问题是,无需创建 ModelSurveysController
,如何销毁 ModelSurvey
,同时保留 Event
和 Survey单独?
带有 map.resources :events, :has_many => 的东西:模型调查
?我不太确定在这种情况下该怎么办。路由需要做什么,控制器需要做什么?我希望网址看起来像这样:
/events/:title/model_surveys/:id
感谢您的帮助, 槊
How do I destroy the association itself and leave the objects being associated alone, while keeping this RESTful?
Specifically, I have these models:
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
That's saying that the Event is :surveyable
(ModelSurvey belongs_to Event
). My question is, without having to create a ModelSurveysController
, how do I destroy the ModelSurvey
, while leaving the Event
and Survey
alone?
Something with map.resources :events, :has_many => :model_surveys
? I'm not quite sure what to do in this situation. What needs to happen with the routes, and what needs to happen in the controller? I'm hoping the url could look something like this:
/events/:title/model_surveys/:id
Thanks for your help,
Lance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 2.3 中,您可以使用
accepts_nested_attributes_for
来将 ModelSurveys 数组传递给相关事件。如果您允许通过嵌套属性声明进行销毁,则您将能够传递event[model_surveys][1][_destroy]=1
并且关联将被删除。查看 API 文档。In Rails 2.3 you have
accepts_nested_attributes_for
which would let you pass an array of ModelSurveys to the event in question. If you allow destroy through the nested attributes declaration, you'll be able to passevent[model_surveys][1][_destroy]=1
and the association will be removed. Check out the api docs.资源域!=模型域
控制器的域与模型的域不同。通过更改资源的状态来更新多个模型是完全可以的。
在您的情况下,这意味着对事件或调查执行 PUT 或 POST,其中包含另一个的 id 列表。一个模型将更新关联。
PUT 或 POST
有些人(但不是罗伊·菲尔丁 )认为您应该使用 PUT 来更新资源并再次提供所有状态,其他人则认为具有部分状态(ala PATCH)的 POST 就足够了。
Resources domain != model domain
The domain of the controller is not the same as that of the models. It's perfectly fine to update multiple models by changing the state of a resource.
In your case that means doing a PUT or POST to either the Event or the Survey which contains a list of ids for the other. The model for one will update the association.
PUT or POST
Some people (but not Roy Fielding) believe that you should use a PUT to update the resource and provide all of the state again, others feel that a POST with the partial state (ala PATCH) is sufficient.