Rails中表单用的比较多,其中
formtastic是一个比较简单的构建表单的gem.我个人用了一阵子,不太喜欢它生成的标签,后来发现了
Simple_form,相比formtastic,可以更好自定义标签,有更大的灵活性。
关于 formtastic ,railscasts上有相关教程,可参考
railscasts
1.安装gem
sudo gem install simple_form
2.修改Gemfile:
gem "simple_form"
3.运行generator,会生成所需文件
rails generate simple_form:install
#生成文件
create config/initializers/simple_form.rb
create config/locales/simple_form.en.yml
create lib/templates/erb/scaffold/_form.html.erb
4.使用,以User model为例
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password%>
<%= f.input :email%>
<%= f.input :profile,:as=>:text%> #as使用
<%= f.input :age, :collection => 18..60 , :prompt => "Select your age"%> #collection使用
<%= f.simple_fields_for :adress do |adress_form| %> #simple_fields_for使用
<%= adress_form.input :name %>
<% end %>
<%= f.button :submit %>
<% end %>
如果 不想生成 label,可以使其设置为false
<%= f.input :username,:label=>false %>
同样,如果想使required为false,
<%= f.input :username, :required => false %>
5.关于as
在activerecord下,会自动对应相关映射,例如:string生成单行文本,text生成多行文本,不过如果使用mongodb,我使用的是mongoid,没有text
类型,全是string,所以要自己手动as,例如
<%= f.input :profile,:as=>:text%>
6. Associations 使用
class User < ActiveRecord::Base
belongs_to :company
has_and_belongs_to_many :roles
end
class Company < ActiveRecord::Base
has_many :users
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
表单中我们可以使用
<%= simple_form_for @user do |f| %>
<%= f.input :name %>
<%= f.association :company %>
<%= f.association :roles %>
<%= f.button :submit %>
<% end %>
7.配置 config/initializers/simple_form.rb
Wrapper,如果想修改生成的标签为P(默认为div),只要修改
SimpleForm.wrapper_tag = :p
默认情况下
config.components = [ :label_input, :hint,:error]
如果不想使用error,只要去掉 即可
config.components = [ :label_input, :hint]
更多配置可自行参考文件
8.国际化,只要修改对应本地化文件即可,
zh:
simple_form:
labels:
user:
username: '用户名'
password: '密码'
hints:
user:
username: '登录用户名.'
password: '输入正确的字符.'
placeholders:
user:
username: '你的用户名'
password: '****'
9. 和formtastic的比较
我个人比较喜欢 simple_form,生成的代码简洁些,更加灵活,可自定义性强。
10.textmate支持
formtastic:
formtastic_tmbundle
simple_form:
simple_form_tmbundle
-------
继续打标记:
转载注明:
javaeye--
doabit..
----------------------------
分享到:
相关推荐
Simple Form的基本目标是不触及定义布局的方式,而是让您为眼睛找到更好的设计。 大多数DSL都是从Formtastic继承的,我们对此表示感谢,并应使您感到宾至如归。 信息:本自述文件指的是简单表格5.0。 对于较早的...
Cocoon与表单生成器无关,因此可与标准Rails,Formtastic或SimpleForm一起使用。 它与导轨3、4和5兼容。该项目与Apache Cocoon不相关。 先决条件此gem取决于jQuery,因此在您已经在使用jQuery的Rails项目中,它最...
简单的jQuery Form Builder 一个简单的,以jQuery为重点的表单生成器插件,易于修改,因此适合您项目的特定需求。 在PHP中使用将表单保存到数据库或从数据库加载表单的示例,但是不需要PHP。 详细信息: : 演示: ...
开始。 简单的表单生成器。 要添加新的用户信息块,请单击“添加新信息块”:\n - 块位置的行号 - 块的标签 要为任何块添加新属性,请单击“添加新属性”:- 键入块名称,位置属性 - 属性名称(或标签) - 属性类型...
Goutam singh的Form Builder 用Javascript编写的Simple Form Builder支持Dualway绑定和Json Export 利用车把,Way.js和JQuery 支持自定义工具
简单生成器 这是什么? 这只是一个受启发的简单表单构建器。 我是在一家公司进行技术测试的,所以这只不过是对我的技能的测试。 我使用和做到了这一点。 执照 麻省理工学院执照
WindowBuilder is a powerful and easy to use bi-directional Java GUI designer that makes it very easy to create Java GUI applications without spending a lot of time writing code to display simple forms...
WindowBuilder is a powerful and easy to use bi-directional Java GUI designer that makes it very easy to create Java GUI applications without spending a lot of time writing code to display simple forms...
Rails 设计 架构: : 首选项: 吉特:真的 应用程序4:导轨设计 认证:设计 授权:假 Better_errors: 真 部署:无 local_env_file: 假 ...quiet_assets:真实 ...form_builder: simple_form rvmrc:真的 执照 在路上
FreeForm与大多数表单gem兼容,包括simpleform和formbuilder FreeForm不能与Ryan Bate的nested_form gem一起使用,但提供其自身相同的行为请使用/迁移到该gem的2.x版本。 将不再支持1.x版,而2.x版则提供了更好的...
WindowBuilder is a powerful and easy to use bi-directional Java GUI designer that makes it very easy to create Java GUI applications without spending a lot of time writing code to display simple forms...
WindowBuilder is a powerful and easy to use bi-directional Java GUI designer that makes it very easy to create Java GUI applications without spending a lot of time writing code to display simple forms...
LABEL &X-Form... MESSAGE Edits poles and points of splines and surfaces. SYNONYMS pole editing, control point editing, surface editing, b-surface editing, spline editing, curve editing, shape ...
Simple Form的基本目标是不触及定义布局的方式,而是让您轻松找到be Rails表单。 简单表单旨在在帮助您使用功能强大的组件创建表单的同时尽可能地灵活。 Simple Form的基本目标是不触及定义布局的方式,而是让您为...
VCLSkin is a component to create skinnable user interface for Delphi/C++Builder application, It is easy to use, just put one component on mainform, Vclskin will skin whole application without source ...
32. 用PowerBuilder访问Lotus Notes数据库 55 33. 如何在表单中加入计数器 58 34. 实现两个数据库间的数据追加 C/S 结构 60 35. 如何防止他人使用旧id和旧口令访问Domino服务器? 61 36. 在你的WEB站点上使用DOMINO...
News in 3.06*Fix bug in C++Builder.*Fix bug in scrollbar resize.*Fix bug in Delphi7 XPmanifest component.News in 3.05*Fix bug when move controls in different forms.*Fix problem in TStatusbar that is ...
Backend Page Builder based on Visual Composer (1) Custom Plethora Shortcodes and VC Row Settings WooThemes Flexslider custom integration WooCommerce Ready to build a simple e-shop WPML Ready to create...
32. 用PowerBuilder访问Lotus Notes数据库 55 33. 如何在表单中加入计数器 58 34. 实现两个数据库间的数据追加 C/S 结构 60 35. 如何防止他人使用旧id和旧口令访问Domino服务器? 61 36. 在你的WEB站点上使用DOMINO...
32. 用PowerBuilder访问Lotus Notes数据库 55 33. 如何在表单中加入计数器 58 34. 实现两个数据库间的数据追加 C/S 结构 60 35. 如何防止他人使用旧id和旧口令访问Domino服务器? 61 36. 在你的WEB站点上使用DOMINO...