【问题标题】:Automatic Formbuilder for Mongoid 2.0 ? RailsMongoid 2.0 的自动表单生成器?导轨
【发布时间】:2011-10-02 09:42:30
【问题描述】:

是否有 Mongoid 2.0 的表单生成器? 它会自动从模型生成表单。

谢谢

【问题讨论】:

    标签: ruby-on-rails mongoid formbuilder


    【解决方案1】:

    为什么不直接使用 Rails sacffolding?

    【讨论】:

    • 因为脚手架是为入门而设计的,而不是为构建naked-objects 应用程序而设计的。
    • 不,这是因为当您在模型上添加新字段时,它不会自动将字段添加到_form.html.erb。使用 MONGOID,您不必再为模型上添加的每个字段运行新的迁移。这有点神奇:) web2py.com 所做的事情。
    【解决方案2】:

    https://github.com/mcasimir/document_form

    gem 文档格式

    这是我从https://github.com/justinfrench/formtastic 制作的一个叉子,刚刚移到 Mongoid 2。

    型号

      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
    

    查看

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

    这是一个基本示例:此处表单构建器将按照声明的顺序呈现字段,跳过具有:private =&gt; true 的字段。

    如果您不着急并且想要更灵活的东西,您始终可以使用与 formtastic 相同的语法来指定字段广告选项,如下所示:

      <% 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 %>
    

    如果您决定试一试,我将不胜感激任何形式的反馈。

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2014-08-23
      相关资源
      最近更新 更多