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

xisi · September 18, 2013 · Last by XiSi replied at September 22, 2013 · 2977 hits
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 这个地方是一个坑,不可省

You need to Sign in before reply, if you don't have an account, please Sign up first.