RESTful 破坏 Rails 中的多态关联?

发布于 2024-08-21 01:21:17 字数 872 浏览 5 评论 0原文

如何销毁关联本身并保留关联的对象,同时保持 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,同时保留 EventSurvey单独?

带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-08-28 01:21:17

在 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 pass event[model_surveys][1][_destroy]=1 and the association will be removed. Check out the api docs.

昨迟人 2024-08-28 01:21:17

资源域!=模型域

控制器的域与模型的域不同。通过更改资源的状态来更新多个模型是完全可以的。

在您的情况下,这意味着对事件或调查执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文