如何将Ruby嵌入Bootstrap Select Form?

时间:2014-12-29 20:44:32

标签: ruby-on-rails ruby twitter-bootstrap

我该如何使这项工作?下拉表单显示在页面上,但类别只是在它下面笨拙地浮动,单击表单什么都不做。

_form.html.erb



<form>
    <select class="form-control">
    <%= f.collection_select :categories, Value::VALUES, :to_s, :to_s, :include_blank => true, class: 'form-control', placeholder: 'Choose Category' %>
  </select>
</form>
&#13;
&#13;
&#13;

value.rb模型

class Value < ActiveRecord::Base
    belongs_to :user

    VALUES = ['Courage', 'Determination', 'Gratitude', 'Humor']

end

整个表单

&#13;
&#13;
<%= form_for(@value) do |f| %>
  <% if @value.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@value.errors.count, "error") %> prohibited this value from being saved:</h2>

      <ul>
      <% @value.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="america">
<form>
  <div class="form-group">
    <%= f.text_field :name, class: 'form-control', placeholder: 'Enter Value' %>
  </div>
  <select class="form-control">
    <%= f.collection_select :categories, Value::VALUES, :to_s, :to_s, :include_blank => true, class: 'form-control', placeholder: 'Choose Category' %>
  </select>


  <%= button_tag(type: 'submit', class: "btn") do %>
  <span class="glyphicon glyphicon-plus"></span>
  <% end %>

  <%= link_to values_path, class: 'btn' do %>
  <span class="glyphicon glyphicon-chevron-left"></span>
  <% end %>

  <%= link_to @value, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
  <span class="glyphicon glyphicon-trash"></span>
  <% end %>
  </form>
</div>
<% end %>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

查看API docs for form_for

<%= form_for @value do |f| %>
    <%= f.select :value, Value::VALUES %>
<% end %>

修改

为您的示例修改。我不确定使用常量是否有效。您可能需要将这些值传递给控制器​​中的实例或类变量。