开发者

Automatic Formbuilder for Mongoid 2.0 ? Rails

开发者 https://www.devze.com 2023-04-10 05:53 出处:网络
Is there a formbuilder for Mongoid 2.0 ? Wh开发者_开发问答ich generates automatically a form from a model.

Is there a formbuilder for Mongoid 2.0 ? Wh开发者_开发问答ich generates automatically a form from a model.

Thanks


Why not just use Rails sacffolding?


https://github.com/mcasimir/document_form

gem document_form

It's a fork from https://github.com/justinfrench/formtastic i've made, just moved to Mongoid 2.

Model

  class Person
    include Mongoid::Document
    include Mongoid::MultiParameterAttributes

    validates_presence_of :name

    field :name
    field :secret,            :private => true
    field :birthday,          :type => Date
    field :department_number, :type => Integer, :range => 1..10

    field :description,       :long => true
  end

View

    <% document_form_for @object do |f| %>
      <%= f.inputs %>
      <%= f.buttons %>
    <% end %>

This is a basic example: here the form builder will render the fields in the same order they are declared, skipping those who have :private => true.

If you are not in a hurry and you want something more flexible you can always specify fields ad options using the same syntax as formtastic, something like this:

  <% f.inputs do %>
    <%= f.input :title %>
    <%= f.input :published, :label => "This post is published" %>
    <%= f.input :section_id %>
    <%= f.input :image_filename, :hint => "540x300" %>
  <% end %>

If you'll decide to give it a try it i will appreciate any sort of feedback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号