有些搜索不起作用

时间:2013-12-03 04:58:22

标签: ruby-on-rails ruby-on-rails-3 forms

我想设置三个搜索表单,但三个搜索表单中的两个(名称和地点)不起作用。我不明白为什么它们会有所不同,因为它们的代码几乎相同。

☆members_controller

def index
if !checklogin? then return end #
  if params[:name].present?
   @members = Member.where("name like ?" , "%" + params[:name] + "%")
  else
   @members = Member.all 
  end

  if params[:place].present?
   @members = Member.where("place like ?" , "%" + params[:place] + "%")
  else
   @members = Member.all 
  end

  if params[:field].present?
   @members = Member.where("field like ?" , "%" + params[:field] + "%")
  else
   @members = Member.all 
  end

  respond_to do |format|
   format.html # index.html.erb
   format.json
  end 
 end

☆members_index(视图)

<div class= "form_index">
  <%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
   <div class="from_field_index">
    <%= label_tag 'name', '名前検索:' %>
    <%= text_field_tag 'name' %>
   <%= submit_tag '検索' %>
 <% end %>
  </div>
</div>

<div class= "form_index">
  <%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
   <div class="from_field_index">
    <%= label_tag 'place', '活動場所検索:' %>
    <%= text_field_tag 'place' %>
   <%= submit_tag '検索' %>
  <% end %>
  </div> 
</div>

<div class= "form_index">
  <%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
   <div class="form_field_index">
    <%= label_tag 'field', '言語分野検索:' %>
    <%= text_field_tag 'field' %>
   <%= submit_tag '検索' %>
  <% end %>
  </div>
</div>

☆命令字段搜索

Processing by MembersController#index as HTML
  Parameters: {"utf8"=>"✓", "field"=>"rails", "commit"=>"検索"}
  Member Load (0.4ms)  SELECT "members".* FROM "members" 
  CACHE (0.0ms)  SELECT "members".* FROM "members" 
  Member Load (0.2ms)  SELECT "members".* FROM "members" WHERE (field like '%rails%')
  Member Load (0.1ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  Rendered members/index.html.erb within layouts/application (3.7ms)
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]

☆命令搜索(坏情况)

Started GET "/members?utf8=%E2%9C%93&place=%E6%A8%AA%E6%B5%9C&commit=%E6%A4%9C%E7%B4%A2"     for 127.0.0.1 at 2013-12-03 13:47:20 +0900
Processing by MembersController#index as HTML
  Parameters: {"utf8"=>"✓", "place"=>"横浜", "commit"=>"検索"}
  Member Load (0.2ms)  SELECT "members".* FROM "members" 
  CACHE (0.0ms)  SELECT "members".* FROM "members" 
  Member Load (0.2ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  Rendered members/index.html.erb within layouts/application (6.5ms)
  CACHE (0.0ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1  [["id", 17]]
  Rendered layouts/_header.html.erb (1.6ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 96ms (Views: 73.5ms | ActiveRecord: 1.1ms)

1 个答案:

答案 0 :(得分:1)

我认为您需要保留@members

  # Rails 4
  @members = Member.all 

  # Rails 3
  @members = Member.scoped

  if params[:name].present?
   @members = @members.where("name like ?" , "%" + params[:name] + "%")
  end

  if params[:place].present?
   @members = @members.where("place like ?" , "%" + params[:place] + "%")
  end

  if params[:field].present?
   @members = @members.where("field like ?" , "%" + params[:field] + "%")
  end

注意只有一次拨打all。另请注意,在Rails 4中,您将使用all,而在Rails 3中,您将使用scoped