使用多选字段处理相关对象的semantic_fields_for

发布于 2024-12-14 13:52:22 字数 1436 浏览 7 评论 0原文

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

在我的帖子表单中,我有以下 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? && [email protected]_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 %>

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

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

I have a Post which can have multiple Tags, each of which relates to a User (think Facebook tagging).

In my Post form I have this Formtastic code:

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

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

    <% if @post.tags.present? && [email protected]_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 %>

As you can see this can allow a tag to be added one at a time. However I'd like to allow multiple selections in the dropdown menu, to create multiple tags in one go. Adding "multiple" doesn't work, however: it simply results in creating a tag for the current user, posting the Post.

Can anyone suggest a way I can use a single select field to create multiple tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-12-21 13:52:22

聚会有点晚了,但我使用了很棒的 jQuery Chosen 插件 解决了这个问题,它可以进行多个选择看起来真的很好。

A bit late to the party, but I solved this problem using the awesome jQuery Chosen plugin which makes multiple selects look really good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文