我如何在 rails 中处理这种类型的多级表单 [英] How can I handle this type of multi level forms in rails

查看:42
本文介绍了我如何在 rails 中处理这种类型的多级表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Rails 3.1.我有以下型号

I am in rails 3.1. I have the following models

    class Tool < ActiveRecord::Base
        has_many :comments
    end

    class Comment < ActiveRecord::Base
        belongs_to :tool
        has_many :relationships
        has_many :advantages, :through => :relationships, :source => :resource, :source_type => 'Advantage'
        has_many :disadvantages, :through => :relationships, :source => :resource, :source_type => 'Disadvantage'

    end

    class Relationship < ActiveRecord::Base
        belongs_to :comment
        belongs_to :resource, :polymorphic => true
    end

    class Disadvantage < ActiveRecord::Base
        has_many :relationships, :as => :resource
        has_many :comments, :through => :relationships
    end

    class Advantage < ActiveRecord::Base
        has_many :relationships, :as => :resource
        has_many :comments, :through => :relationships
    end

简而言之,一个Tool有很多comments.Comment 反过来与 AdvantagesDisadvantages 相关联.所以在我的 tool/show 页面中,我会列出所有的评论.

In short, A Tool has many comments. A Comment inturn is associated with Advantages and Disadvantages. So in my tool/show page, I would list out all the comments.

但是如果我必须在工具页面上添加评论,就会有一个表单,其中有一个用于评论的文本区域和两个用于优缺点的多选列表框.

But if I have to add comment to the tool page, there would be a form which has a textarea for comment and two multi select list boxes for advantages and disadvantages.

这里有一个问题,如果用户想从现有的 adv/disadv 中选择,用户可以从列表框中选择,或者如果用户想要添加一个新的 adv/disadv,他可以输入并添加它,所以它是通过 ajax 调用保存的,并且新的 adv/disadv 被添加到列表框中.我该怎么做?

And here is the catch, if the user wanna select from existing adv/disadv, the user can select from the list box or if the user wants to a add a new adv/disadv he can type it and add it, so that it is saved thru an ajax call and the new adv/disadv is added to the list box. How am I supposed to do this?

推荐答案

您正在寻找的是嵌套表单" - 它们非常易于使用.

What you're looking for are "nested forms" - they are really straight-forward to use.

在您的 Gemfile 中添加:

in your Gemfile add:

gem "nested_form"

1) 在您的 main_model 中,您将包含对 accepts_nested_attributes_for :nested_model

class MainModel
  accepts_nested_attributes_for :nested_model
end

2) 在 main_model 的视图中 而不是 form_for() ,您将在顶部调用 nested_form_for()

2) in the view for your main_model instead of form_for() , you will call nested_form_for() at the top

= nested_form_for(@main_model) do |f|
   ...

检查该方法的 Rails 页面,它有一些有趣的选项,例如:reject_if , :allow_destroy, ...

Check the Rails page for that method, it has interesting options, e.g. :reject_if , :allow_destroy, ...

3) 在 main_model 的视图中,当你想显示嵌套模型的子表单时,你会这样做

3) in the view for the main_model, when you want to show a sub-form for the nested model, you will do

= f.fields_for :nested_model   # replace with your other model name

然后它将只使用 _form 部分作为嵌套模型并将其嵌入到 main_model 的视图中

it will then just use the _form partial for the nested_model and embed it in the view for the main_model

很有魅力!

查看这些 RailsCast.com 剧集,其中深入介绍了嵌套表单:

http://railscasts.com/episodes/196-nested-模型形式部分 1

http://railscasts.com/episodes/197-nested-模型形式第 2 部分

希望能帮到你

这篇关于我如何在 rails 中处理这种类型的多级表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆