在循环中添加复选框 - Rails 4

时间:2016-12-15 19:58:32

标签: jquery ruby-on-rails ruby model-view-controller

我有一个agent模型,我试图在每个代理的名称旁边添加一个复选框,以便可以用来搜索。我有点矮了。下面是我目前的代码,但不知道如何输入名称。现在我只是得到了一堆复选框。

html.erb:

<a tabindex="0" class="button btn-transparent" id="listing-agent-selector" role="button" data-toggle="popover">Agents <span class="caret"></span></a>

<div id="listing-agent-popover" style="display: none;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-6">
        <div class="input-group input-group-sm">
          <% @agents.each do |agent| %>
            <%= check_box_tag :agent_id_list, class: "form-control", placeholder: "Agent", data: { "binding-name" => "agent_id_list" } %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

JS:

$('#listing-agent-selector').popover( {
  html: true,
  trigger: 'manual',
  placement: 'bottom',
  template:  '<div class="popover price-range-pop" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>',
  content: function() {
    return $('#listing-agent-popover').html();
  }
});

search_for.rb:

attribute :agent_id_list, String

def agent_ids
  String(agent_id_list).split(',')
    .reject(&:blank?)
end

def agent_id_list_scope
  if agent_ids.empty?
    Listing.all
  else
    Listing.where("listing_agent_id IN (?) OR sales_agent_id IN (?)", agent_ids, agent_ids)
  end
end

目前的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

我看一下,看看这是否有助于你解决问题

我所做的是为每个代理添加一行以显示

旁边的名称
<a tabindex="0" class="button btn-transparent" id="listing-agent-selector" role="button" data-toggle="popover">Agents <span class="caret"></span></a>

<div id="listing-agent-popover" style="display: none;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-6">
        <div class="input-group input-group-sm">
          <% @agents.each do |agent| %>
            <div class="col-xs-1">
              <%= check_box_tag :agent_id_list, class: "form-control", placeholder: "Agent", data: { "binding-name" => "agent_id_list" } %>
            </div>
            <div class="col-xs-11">
              <%= @agent.name %>
            </div>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

这是要查找的块

      <% @agents.each do |agent| %>
        <div class="col-xs-1">
          <%= check_box_tag :agent_id_list, class: "form-control", placeholder: "Agent", data: { "binding-name" => "agent_id_list" } %>
        </div>
        <div class="col-xs-11">
          <%= @agent.name %>
        </div>
      <% end %>