Rails 3嵌套表单 - 新对象

时间:2012-11-23 23:39:11

标签: ruby-on-rails-3 nested-forms simple-form

我不记得代码,如何以嵌套形式创建新对象(通过简单形式)..它类似于:“something:new_object ...”

由于

2 个答案:

答案 0 :(得分:0)

通常使用:

<%= f.fields_for :object do |builder| %>

但您可以使用 simple_fields_for ,如下所示:

form_for @user do |f|
  f.simple_fields_for :posts do |posts_form|
    # Here you have all simple_form methods available
    posts_form.input :title
  end
end

参考:http://simple-form.plataformatec.com.br/#usage/extra-helpers

最初使用 form_for ,但请检查此主题:nested attributes in simple_form returns mass assignment error

答案 1 :(得分:0)

感谢Dropbox,我找到了它。

您需要将此添加到您的javascript(使用CoffeScript)

#= require jquery_nested_form

这就是形式(在HAML中)

= simple_nested_form_for @variable do |f|
  = f.input :code
  // Link to create new empty object    
  = f.simple_fields_for :nested_attributes do |s|
    = f.link_to_add "Add new", :nested_attributes
    = s.input :name
    = s.input :locale
    // Link to remove
    = s.link_to_remove 'Remove'
  = f.button :submit