【问题标题】:Rails: Using Bootstrap Input Style to Replace the built-in form_for method in RailsRails:使用 Bootstrap 输入样式替换 Rails 中内置的 form_for 方法
【发布时间】:2012-11-15 01:29:38
【问题描述】:

我正在尝试在 Rails 中使用 Bootstrap forms。但是在检查了我的 post 脚手架创建的 _form 部分后,我不太确定如何使用常规输入法编辑变量。

例如,这是内置的表单代码:

<%= form_for(@post) do |f| %>

<div class="field">
 <%= f.label :name %><br />
 <%= f.text_field :name %>
</div>
<div class="field">
 <%= f.label :content %><br />
 <%= f.text_area :content %>
</div>
 <div class="actions">
 <%= f.submit %>
</div>

<% end %>

我想使用下面的表单代码:

<form>
<fieldset>
<legend>Start posting</legend>
<label>Name</label>
 <input class="span10" type="text" placeholder="Name: ">
<label>Content</label>
 <input class="span10" type="text" placeholder="Content: ">
<textarea rows="10"></textarea>
<button type="submit" class="btn">Submit</button>
</fieldset>
</form>

问题:如何使用 Bootstrap 版本的表单代码?

我试过类似..

<input class="span10" type="text" name="@post.name" placeholder="Type in name: ">
<button type="submit" class="btn">Submit</button>

但它似乎不起作用。我认为 Bootstrap 提交按钮没有指向正确的方向,所以我也尝试了以下操作:

<input class="span10" type="text" name="@post.name" placeholder="Type in name: ">
<%= form_for(@post) do |f| %>
 <%= f.submit %>
<% end %>

有人可以帮我解决这个问题吗?感谢您的帮助!

【问题讨论】:

  • 你在使用 twitter-bootstrap gem 吗?
  • 没有。是否建议这样做?我刚刚下载了 Bootstrap 的编译版本并将它们放入我的资产文件夹中。到目前为止,我还没有发现任何不便。如果我使用 gem,那么在我的帖子中使用上述表单有何不同?

标签: html css ruby-on-rails forms


【解决方案1】:

您应该将 twitter-bootstrap-rails gem 添加到您的应用程序中,而不是手动执行此操作。请在此处获取说明和文档:twitter-bootstrap-rails gem

这样就行了!

【讨论】:

  • 安装gem后,form_for方法会自动使用bootstrap版本的输入法吗?如果我也可以学习手动方式,我将不胜感激..
  • 对于 Rails 项目,手动操作是没有意义的。检查我发布给您的链接中的文档和截屏视频。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 2015-01-13
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多