Ruby on Rails是否具有与Django的ModelForm相同的功能?

时间:2021-08-10 12:19:01

I have been using Django for about a year and thought I would try Ruby on Rails to see how it compares. In Django, you can create a ModelForm that automatically generates a html for based on the types of fields in your model. Is there a class that does something similar in Rails? I know scaffolding will generate a erb file for my model, but is there a way to do this?

我使用Django已经有一年了,我想尝试一下Ruby on Rails,看看它是如何比较的。在Django中,可以创建一个模型表单,该表单根据模型中的字段类型自动生成html。在Rails中有类似的类吗?我知道脚手架将为我的模型生成一个erb文件,但是有办法做到这一点吗?

<%= auto_form_for @person %>

< % = auto_form_for @ person % >

Or this?

还是这个?

<%= form_for @person do |f| %>
<% for each @person.fields do | field| %>
<%= auto_field f, field %>
<%end%>
<% end %>

<%= form_for @person do |f| %> <% for each @person。字段做|字段|% > <%= auto_field f,字段%> <%end%> <%end%>

I apologize for my poor ruby code.

我为我可怜的ruby代码道歉。

2 个解决方案

#1


2  

Formtastic implements the Django pony magic you're looking for, and most variants thereof that might actually be useful. In my opinion, most end-user apps don't find "auto forms" and scaffolds to be very useful (since they so often need to be customized in detail), but Formtastic may output enough semantic HTML for you to style in a way that works for your users.

Formtastic实现了您正在寻找的Django小马魔术,并且它的大多数变体可能实际上是有用的。在我看来,大多数终端用户应用程序并不认为“自动表单”和scaffold是非常有用的(因为它们经常需要进行详细的定制),但是Formtastic可能会输出足够多的语义HTML供您使用,以适合您的用户。

Rails also allows you to write your own custom form builders; you can also extend the ones Formtastic provide.

Rails还允许您编写自己的自定义表单构建器;您还可以扩展Formtastic所提供的功能。

#2


2  

I found a gem called simple_form that is similar to formtastic. It allows you to to define forms like this:

我发现了一个名为simple_form的宝石,类似于formtastic。它允许你这样定义表单:

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

I've used it for a few projects, and it's worked out great. It also has integration with twitter bootstrap, if you are using that library.

我用它做过几个项目,效果很好。如果您正在使用该库,它还与twitter引导程序集成。

#1


2  

Formtastic implements the Django pony magic you're looking for, and most variants thereof that might actually be useful. In my opinion, most end-user apps don't find "auto forms" and scaffolds to be very useful (since they so often need to be customized in detail), but Formtastic may output enough semantic HTML for you to style in a way that works for your users.

Formtastic实现了您正在寻找的Django小马魔术,并且它的大多数变体可能实际上是有用的。在我看来,大多数终端用户应用程序并不认为“自动表单”和scaffold是非常有用的(因为它们经常需要进行详细的定制),但是Formtastic可能会输出足够多的语义HTML供您使用,以适合您的用户。

Rails also allows you to write your own custom form builders; you can also extend the ones Formtastic provide.

Rails还允许您编写自己的自定义表单构建器;您还可以扩展Formtastic所提供的功能。

#2


2  

I found a gem called simple_form that is similar to formtastic. It allows you to to define forms like this:

我发现了一个名为simple_form的宝石,类似于formtastic。它允许你这样定义表单:

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

I've used it for a few projects, and it's worked out great. It also has integration with twitter bootstrap, if you are using that library.

我用它做过几个项目,效果很好。如果您正在使用该库,它还与twitter引导程序集成。