css - form_for 上的类标签自动化?这种语法的最佳实践是什么?

标签 css ruby-on-rails twitter-bootstrap

我正在使用 bootstrap CSS 开发 Rails App,依赖于 DRY 概念,这种形式可以简化吗?

<%= form_for @user , :html => {:class => "form-horizontal"} do |f| %>
<fieldset>
<legend>Sign Up</legend>
  <div class="form-group">
    <%= f.label :email ,:class=>"control-label" %><br />

    <%= f.text_field :email, :class =>"form-control", :placeholder=>"Email" %>
  </div>
  <div class="form-group">
    <%= f.label :password ,:class=>"control-label" %><br />

    <%= f.password_field :password, :class =>"form-control", :placeholder=>"Password" %>

  </div>
  <div class="form-group">
    <%= f.label :password_confirmation,:class=>"control-label" %><br/>

    <%= f.password_field :password_confirmation , :class =>"form-control", :placeholder=>"Confirm Password" %>

  </div>
<%= f.submit 'Submit', :class => 'btn btn-primary' %>
</fieldset>
<% end %>

我的意思是像标签 fieldsetlegend:class=>"control-label" 和其他标签。

最佳答案

您需要的是 custom form builder .基本要点是封装字段装饰(在许多情况下甚至是标签),以便您获得与上面代码类似的输出,如下所示:

<%= form_for @user, builder: MyFormBuilder do |f| %>
  <%= f.input :email %>
  <%= f.input :password %>
  <%= f.input :password_confirmation %>
  <%= f.button :submit %>
<% end %>

many tutorials on how to build your own ,它非常方便,是了解更多信息的好方法。如果您想要准备好的解决方案,请查看 the simple_form gem .

关于css - form_for 上的类标签自动化?这种语法的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25471583/

相关文章:

css - 如何在 Bootstrap 中将 div 放在 .container 的右边?

css - 在 Bootstrap 列周围添加一致的 1px 边框

javascript - jQuery 切换幻灯片不会向后滑动

css - jquerymobile ajax问题

php - Wordpress 不会在所有页面上显示导航菜单

ruby-on-rails - 例子?多模型嵌套表单及处理,Rails3+Datamapper

javascript - Bootstrap - 直接链接到模态窗口

html - sendmailR 在 HTML 正文中添加额外空间,偶尔会破坏 HTML 标签

css - Heroku 上的 twitter-bootstrap-rails : Glyphicons displayed as squares

ruby-on-rails - Golang 微服务和与现有应用程序的通信