开发者

Does Ruby on Rails have an equivalent to Django's ModelForm?

开发者 https://www.devze.com 2023-02-10 17:59 出处:网络
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

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?

<%= auto_form_for @person %>

Or this?

<%= form_for @person do |f| %>

<% for each开发者_C百科 @person.fields do | field| %>

<%= auto_field f, field %>

<%end%>

<% end %>

I apologize for my poor ruby code.


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.

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


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

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

0

精彩评论

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

关注公众号