使用sinatra复选框过滤搜索

时间:2015-02-25 18:52:07

标签: ruby-on-rails ruby twitter sinatra

search.rb

keywords = ["sheffield", "liverpool", "london", "manchester"]

get '/search' do

  @keyword = "No keyword entered"
  @results = []
  puts keywords[0]

  unless params[:filter].nil?
      @tick_box1 = params[:filter][:follower] == 'on'
      @tick_box2 = params[:filter][:following] == 'on'
  end

  if (@tick_box1) && (@tick_box2)
    @results = @client.search('chelsea').take(20)
  elsif (@tick_box1) && (!@tick_box2)
    @results = @client.search('arsenal').take(20)
  elsif (!@tick_box1) && (@tick_box2)
    @results = @client.search('liverpool').take(20)
  else
    unless params[:keyword].nil?
      @keyword = params[:keyword]
      @results = @client.search(@keyword).take(20)
    end

    unless params[:use_keyword].nil?
      @results = @client.search(keywords[0]).take(20)
    end
  end
    erb :search
end

Search.erb

<% query = "" %>
<h1>Search</h1>

<div class="search-container">
    <form>
        <input type="search" name="keyword" value="<%=h params[:query]%>">
        <input type="submit" value="Search" class="button">
        <a href="?use_keyword=1" class="button">Use Keywords</a>
        <a href="settings">Edit Keywords</a>

        <div class="filters">
            <ul>
                <li><input type="checkbox" name="filter[follower]">Followers</li>
                <li><input type="checkbox" name="filter[folowing]">Following</li>
            </ul>
        </div>
    </form>


    <% if query != "" %>
    <p>Showing results for <strong><%=h query %></strong></p>
    <% end %>
    <%= @keyword %>
    <ol class="search-results">
        <% @results.each do |result| %>
        <li><%= result.text %></li>
        <% end %>
    </ol>
    <%= erb :"partials/pagination", :locals => {
        :total_results => 100,
        :results_per_page => 10,
        :current_page => 1
    }%>

我想这样做,当选中复选框时,它会搜索某个类别(例如关注者)。但即使我检查勾选框,它仍然会搜索输入的关键字。但是如果我没有选中任何复选框,我只希望它在整个Twitter上搜索输入的关键字。有没有人有任何解决方案。

ps(我将编写搜索关注者的代码,稍后当我发现这一点时会跟进。)

2 个答案:

答案 0 :(得分:1)

问题出在您看来,您有另一种形式的复选框,因此当您点击搜索时,它们不会被发送。您需要一种形式的所有输入。

答案 1 :(得分:0)

search.rb

 keywords = ["sheffield", "liverpool", "london", "manchester"]

get '/search' do

  @keyword = "No keyword entered"
  @results = []
  puts keywords[0]

  unless params[:filter].nil?
      @tick_box1 = params[:filter][:follower] == 'on'
      @tick_box2 = params[:filter][:following] == 'on'
  end

  if (@tick_box1) && (@tick_box2)
    @results = @client.search('chelsea').take(20)
  elsif (@tick_box1) && (!@tick_box2)
    @results = @client.search('arsenal').take(20)
  elsif (!@tick_box1) && (@tick_box2)
    @results = @client.search('liverpool').take(20)
  else
    unless params[:keyword].nil?
      @keyword = params[:keyword]
      @results = @client.search(@keyword).take(20)
    end

    unless params[:use_keyword].nil?
      @results = @client.search(keywords[0]).take(20)
    end
  end
    erb :search
end

<% query = "" %>
<h1>Search</h1>

search.erb

<div class="search-container">
    <form>
        <input type="search" name="keyword" value="<%=h params[:query]%>">
        <input type="submit" value="Search" class="button">
        <a href="?use_keyword=1" class="button">Use Keywords</a>
        <a href="settings">Edit Keywords</a>

        <div class="filters">
            <ul>
                <li><input type="checkbox" name="filter[follower]">Followers</li>
                <li><input type="checkbox" name="filter[following]">Following</li>
            </ul>
        </div>
    </form>


    <% if query != "" %>
    <p>Showing results for <strong><%=h query %></strong></p>
    <% end %>
    <%= @keyword %>
    <ol class="search-results">
        <% @results.each do |result| %>
        <li><%= result.text %></li>
        <% end %>
    </ol>
    <%= erb :"partials/pagination", :locals => {
        :total_results => 100,
        :results_per_page => 10,
        :current_page => 1
    }%>