垂直对齐表单助手

时间:2013-02-16 09:43:15

标签: css ruby-on-rails

我正在使用以下代码生成表单。目前,没有任何样式,所有CSS文件都是空的。

<h1>New Post</h1>
<%= form_for @post do |f| %>
  <%= f.text_field :title %>
  <%= f.text_area :body %>
  <%= f.submit %>
<% end %>

这是application.rb

的内容
<!DOCTYPE html>
<html>
<head>
  <title>Bloog</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
  <div class="sidebar two columns">
    <nav>
      <ul>
        <li><%= link_to "New post...", new_post_path %></li>
      </ul>
    </nav>
  </div>
  <div>
    <%= yield %>
 </div>
</body>
</html>

这会生成一个水平对齐的表单,而不是垂直堆叠元素。

horizontal form using default from_for helper

我希望它看起来像这样,这是我期待的默认行为。

how i thought the from was going to render

1 个答案:

答案 0 :(得分:3)

为元素添加一些装箱以对它们进行分组。例如Div或段落,就像在默认的脚手架中完成一样。

像这样使用:

<h1>New Post</h1>
<%= form_for @post do |f| %>
  <p><%= f.text_field :title %></p>
  <p><%= f.text_area :body %></p>
  <p><%= f.submit %></p>
<% end %>