例子?多模型嵌套表单及处理,Rails3+Datamapper

发布于 2024-10-08 16:38:46 字数 576 浏览 2 评论 0原文

我正在寻找示例,但发现没什么可继续的,因为一切似乎都倾向于 ActiveRecord。我了解我所看到的基本概念,并且我正在努力使用 Datamapper(更具体地说是 dm-accepts_nested_attributes)来完成此操作。更多详细信息可以在这里找到:使用 dm-accepts_nested_attributes 和 dm-is-tree 在 Rails 中构建 2 个模型的嵌套表单

我有一个带有一些文本和图像的帖子的两个模型表单,就是这样。这些模型与具有 n 个图像的帖子和属于帖子的图像相关联。

我已经浏览了一堆不同的搜索和博客文章,但没有发现可供查看的项目或示例,并且文档也很少。而且我是一个学习者/初学者/有点密集。我在这里提问有两个原因:解决我当前的问题,以便未来的提问者有地方可看。

以 Rails 形式使用多个模型似乎有些复杂并且不容易完成,或者确实如此,但我还没有弄清楚。

I'm searching for examples, but have found little to go on because everything seems slanted toward ActiveRecord. I understand the basic concepts for what I've seen and I'm working on doing this with Datamapper (dm-accepts_nested_attributes to be more specific). More details can be found here: nested forms for 2 models in rails using dm-accepts_nested_attributes and dm-is-tree

I have a two model form for a Post with some text and an Image, that is it. The models are associated with the Post having n number of images and an image belonging to a post.

I've been over a bunch of different searches and blog posts but there are no projects or examples I've found to look over and documentation is sparse. Also I'm a learner/beginner/somewhat dense. I'm asking here for two reasons: working through my current issue, and so future questioners have a place to look.

Working with multiple models in rails forms seems to be somewhat complex and not easily accomplished, or it is but I haven't figured it out yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南城旧梦 2024-10-15 16:38:46

所以,这是我从类似应用程序(Rails 3 + DataMapper + dm-accepts_nested_attributes + Haml)中提取的一些代码。在我的示例中,我有一个医疗诊所,该诊所有许多administrators_practices,并且通过administrators_practices 有许多管理员。这是模型的相关部分:

has n, :administrators_practices
has n, :administrators, :through => :administrators_practices
accepts_nested_attributes_for :administrators

然后,在我看来,我有这样的东西:

%h1 Practice Setup
%ol
  = form_for @practice do |f|

    %li
      = f.label :name
      = f.text_field :name

    = f.fields_for :administrators do |administrator_builder|

      %li
        = administrator_builder.label :name, "Administrators Name"
        = administrator_builder.text_field :name

      %li
        = administrator_builder.label :email
        = administrator_builder.text_field :email

      %li
        = administrator_builder.label :password
        = administrator_builder.password_field :password

这应该可行。诀窍是使用 fields_for 并创建一个新的表单生成器。然后,您基本上可以将其视为主模型的一部分。

如果你被卡住了,你可以把碎片分开。当我学习将这些东西放在一起时,我会首先处理模型,然后尝试在那里创建一个嵌套模型。然后,我将构建视图,并在控制器内运行调试器,以确保我在 params 对象内获取正确的信息。有时我可以充分利用参数来了解它应该是什么,然后返回并更新我的视图以正确方式构建表单。

So, this is some code I pulled out of a similar application (Rails 3 + DataMapper + dm-accepts_nested_attributes + Haml). In my example, I have a medical practice that has many administrators_practices and has many administrators through administrators_practices. Here's the relevant part of the model:

has n, :administrators_practices
has n, :administrators, :through => :administrators_practices
accepts_nested_attributes_for :administrators

Then, in my view, I have something like this:

%h1 Practice Setup
%ol
  = form_for @practice do |f|

    %li
      = f.label :name
      = f.text_field :name

    = f.fields_for :administrators do |administrator_builder|

      %li
        = administrator_builder.label :name, "Administrators Name"
        = administrator_builder.text_field :name

      %li
        = administrator_builder.label :email
        = administrator_builder.text_field :email

      %li
        = administrator_builder.label :password
        = administrator_builder.password_field :password

That should work. The trick is to use fields_for and create a new form builder. Then, you can treat it basically like part of the main model.

If you get stuck, you can break the pieces apart. When I was learning to put these kinds of things together, I would work first on the model, then try and create a nested model there. Then, I would build my views, and run a debugger inside my controller to make sure I'm getting the right information inside the params object. Sometimes I could play with the params enough to see what it was supposed to be, then go back and update my views to get the form built the right way.

待天淡蓝洁白时 2024-10-15 16:38:46

我希望这篇官方指南能够解答您的问题:
http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

I hope this piece of the official guide answers your question:
http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

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