【问题标题】:Using Rails nested_form without any associated models在没有任何关联模型的情况下使用 Rails nested_form
【发布时间】:2016-02-11 19:53:35
【问题描述】:

使用 Ryan 的 nested_form gem 创建表单。我想要link_to_addlink_to_remove 方法的功能,但是这个表单与我的任何模型都没有任何关系;我根本没有保存它。

= simple_nested_form_for :non_model_object do |f|
  = f.input :input1
  = f.simple_fields_for :non_model_child do |child|
    = child.input :child_input_1
    = child.link_to_remove 'Remove'
  = f.link_to_add 'Add', :non_model_child

non_model_object 显然不是我在数据库中的东西。 non_model_object“有很多”non_model_children 为了这种形式,但同样,这在数据库中也不存在。我收到的错误是:

Invalid association. Make sure that accepts_nested_attributes_for is used for :non_model_child association.

这显然是有道理的,但我不能将它添加到 non_model_object 类中,因为它不存在。

此表单的目的只是让用户填写它,它会向我发送他们输入的 csv。关于如何最好地实现这一点的想法?

谢谢!

导轨 4.2.5.1 红宝石 2.1.2 nested_form 0.3.2

【问题讨论】:

    标签: ruby-on-rails ruby associations nested-forms


    【解决方案1】:

    您可以使用与 Ryan Bates 在截屏视频中使用的方向相同的方向。他举例说明了这些链接的嵌套形式:

    我认为这将有助于解决您的问题。

    【讨论】:

      【解决方案2】:

      你可以使用cocoon gem,这个gem对于嵌套表单来说很简单。

      我在所有项目中都使用这个 gem。 :)

      【讨论】:

      • 如果可能的话,我宁愿坚持使用 nested_form,因为整个应用程序都已经使用它构建了
      • 好的,刚刚介绍了另一种可能性,它极大地促进了开发,并且可以使用各种工具,如 simple form、formtastic、rails pure 等。 Out 允许与 erb、haml 和 Thin 一起使用。我相信这也适用于遇到此问题的其他用户。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多