如何在rails中创建和提交动态相等的搜索字段?

时间:2016-05-24 16:42:56

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-4

我有一个带有下拉框的搜索字段,我可以选择要搜索的列和内容,如下所示:

enter image description here

“+”按钮会添加另一个搜索字段,搜索会发送搜索表单。

我已经拥有获取此字段并将标记附加到它们的函数,使其成为元素数组。问题是,现在我有这个数组并且搜索不起作用,当我提交字段时,另一方没有返回,只有nil类。我试图在live_search中传递一个,并通过所有元素迭代它,但它也不起作用。我认为问题可能在控制器中,但我没有太多经验,所以我不知道该怎么做......

search_engine.html.erb

<div id="search-form">
  <%= button_tag "+", class: "add_field" %>
  <%= form_tag({controller: "people", action: "index"}, method: "get", class: "form_wrapper", remote: true) do %>
    <%= button_tag "search", class: "button_search" %>
  <% end %>
</div>

<script type="text/javascript">

  column =
  '<%  valid_column_names = Person.column_names.reject{|r| r == "created_at" || r == "updated_at" || r == "slug"} %>' +
  '<%= j (select_tag :object_columns, options_for_select(valid_column_names), class: "object_columns") %>' +
  '<%= text_field(:search, :record, autocomplete: :off, class: "search") %>';

  $('button.add_field').on('click',function(){
    $('.form_wrapper').after(column);
    format_fields();
  });

  function format_fields() {
    add_indexes();
    add_ids();
  }

  function add_indexes() {
    add_index_to('object_columns');
    add_index_to('search');
  }

  function add_ids() {
    add_ids_to('object_columns');
    add_ids_to('search');
  }

  function add_index_to(element) {
    var i=0;
    $('.'+element).each(function(){
      i++;
      var newName=element+'['+i+']';
      $(this).attr('name',newName);
    });
  }

  function add_ids_to(element) {
    var i=0;
    $('.'+element).each(function(){
      i++;
      var newID=element+i;
      $(this).attr('id',newID);
    });
  }

</script>

live_search.js.erb

$(function () {

    function search() {
      $.get(window.location.href, { object_columns: $("select#object_columns1").val(), search: $("#search1").val() }, null, "script");
    };

    $('button.button_search').on('click', function(){
      search();
    });
});

searchable.rb

module Searchable
  extend ActiveSupport::Concern

  module ClassMethods
    def search_for(present_column,record)
      record.present? ? where(present_column+' LIKE ?', record+"%") : all
    end
  end
end

people_controller.rb

  def index
    @people = Person.page(params[:page]).per(5).search_for(params[:object_columns], params[:search])
  end

index.html.erb

<p id="notice"><%= notice %></p>

<h1>List of people</h1>

<%= render 'people/search_engine', remote: true %>

<% if @people.exists? %>
  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Total Activity Time</th>
        <th colspan="3"></th>
      </tr>
    </thead>

    <tbody class="people-records">
      <%= render "people_records", remote: true %>
    </tbody>
  </table>
  <%= paginate @people %>
<% else %>
  <p>No People</p>
<% end %>
<p><b>Time to complete all activities:</b> <%= group_total_activities_duration %></p>
<br>

<%= button_to 'Add Person', new_person_path, method: :get %>

index.js.erb的

$(".people-records").html("<%= j render 'people_records' %>") 

登录

对于这个日志,我有两个活动搜索,一个用于ID,另一个用于Name。

Started GET "/people?utf8=%E2%9C%93&button=" for 127.0.0.1 at 2016-05-24 14:07:14 -0300
Started GET "/people?&object_columns=id&search=545&_=1464110597531" for 127.0.0.1 at 2016-05-24 14:23:24 -0300
Processing by PeopleController#index as JS
  Parameters: {"object_columns"=>"id", "search"=>"545", "_"=>"1464110597531"}

  Person Load (0.3ms)  SELECT  "people".* FROM "people" WHERE (id LIKE '545%') LIMIT 5 OFFSET 0
  Rendered people/_people_records.html.erb (1.1ms)
  Rendered people/index.js.erb (5.5ms)
Completed 200 OK in 16ms (Views: 12.9ms | ActiveRecord: 0.3ms)


Started GET "/people?utf8=%E2%9C%93&button=" for 127.0.0.1 at 2016-05-24 14:23:26 -0300
Processing by PeopleController#index as JS
  Parameters: {"utf8"=>"✓", "button"=>""}
  Person Load (0.3ms)  SELECT  "people".* FROM "people" LIMIT 5 OFFSET 0
   (0.1ms)  SELECT SUM("house_activities"."duration") FROM "house_activities" INNER JOIN "assignments" ON "house_activities"."id" = "assignments"."house_activity_id" WHERE "assignments"."person_id" = ?  [["pers
on_id", 5]]
   (0.2ms)  SELECT SUM("house_activities"."duration") FROM "house_activities" INNER JOIN "assignments" ON "house_activities"."id" = "assignments"."house_activity_id" WHERE "assignments"."person_id" = ?  [["pers
on_id", 6]]
   (0.2ms)  SELECT SUM("house_activities"."duration") FROM "house_activities" INNER JOIN "assignments" ON "house_activities"."id" = "assignments"."house_activity_id" WHERE "assignments"."person_id" = ?  [["pers
on_id", 7]]
   (0.1ms)  SELECT SUM("house_activities"."duration") FROM "house_activities" INNER JOIN "assignments" ON "house_activities"."id" = "assignments"."house_activity_id" WHERE "assignments"."person_id" = ?  [["pers
on_id", 8]]
   (0.1ms)  SELECT SUM("house_activities"."duration") FROM "house_activities" INNER JOIN "assignments" ON "house_activities"."id" = "assignments"."house_activity_id" WHERE "assignments"."person_id" = ?  [["pers
on_id", 9]]
  Rendered people/_people_records.html.erb (15.6ms)
  Rendered people/index.js.erb (20.4ms)
Completed 200 OK in 29ms (Views: 26.8ms | ActiveRecord: 1.0ms)

0 个答案:

没有答案