Ruby 使用 accepts_nested_attributes_for 是不是可以解决问卷调查的模型实现

xisi · 2013年09月18日 · 最后由 XiSi 回复于 2013年09月22日 · 2975 次阅读
class Category
  include Mongoid::Document
  include Mongoid::Paperclip
  include Mongoid::Tree
  include Mongoid::Tree::Ordering

  field :title, :type => String
  field :desc,  :type => String, :default => ''

  has_many :books, :autosave => true

  has_mongoid_attached_file :img
  attr_accessible :img, :books_attributes, :title, :desc
  accepts_nested_attributes_for :books, :allow_destroy => true
end
.page-model-form
  = form_for @category do |f|
    .field.need
      %label title
      =f.text_field :title

    .field
      %label desc
      =f.text_field :desc

    .feild
      %label img
      =f.file_field :img

    .field.books
      .book
        - book = @category.books.build

        = f.fields_for :books, book do |book_form|
          %label name
          =book_form.text_field :name

          %label desc
          =book_form.text_field :desc

    .field
      =f.submit

一份问卷中,有多道题的模型

:autosave => true 这个地方是一个坑,不可省

需要 登录 后方可回复, 如果你还没有账号请 注册新账号