【问题标题】:Using a multiple select field to handle semantic_fields_for related objects使用多选字段处理相关对象的语义字段
【发布时间】:2011-11-09 10:12:18
【问题描述】:

我有一个Post,它可以有多个Tags,每个都与一个User 相关(想想Facebook 标记)。

在我的 Post 表单中,我有这个 Formtastic 代码:

<%= f.inputs :class => 'tags' do %>

    <ul>
    <%= f.semantic_fields_for :tags do |t| %>   
        <% if t.object.new_record? %>
            <%= t.input :user_id, :label => "&nbsp;", :input_html => { :class => 'chosen', :'data-placeholder' => 'Select connection' }, :as => :select, :collection => current_user.connections %>
        <% end %>
    <% end %>   
    </ul>

    <% if @post.tags.present? && !@post.new_record? %>
        <ul class="existing-tags">
        <%= f.fields_for :tags do |t| %>
            <% unless t.object.new_record? %>
                <li>
                    <%= link_to avatar(t.object.user), user_path(t.object.user) %>
                    <%= t.check_box :_destroy %>
                    <%= t.label :_destroy, 'Remove' %>
                </li>
            <% end %>
        <% end %>
        </ul>
        <% end %>

<% end %>

如您所见,这可以允许一次添加一个标签。但是,我想允许在下拉菜单中进行多项选择,以便一次创建多个标签。但是,添加“多个”不起作用:它只会为当前用户创建一个标签,发布帖子。

谁能建议我可以使用单个选择字段来创建多个标签的方法?

【问题讨论】:

  • 我也对此问题的解决方案感兴趣。

标签: ruby-on-rails-3 has-many formtastic fields-for


【解决方案1】:

聚会有点晚了,但我使用了很棒的jQuery Chosen plugin 解决了这个问题,这使得多选看起来非常好。

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2015-09-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2020-12-18
    • 1970-01-01
    相关资源
    最近更新 更多