Rails搜索 - 传递参数,不返回结果(错误)

时间:2014-08-09 02:04:23

标签: ruby-on-rails ruby search query-parameters

我试图通过简单的搜索来处理我的Rails应用。我只有两个选择器可供选择:(a)种类和(b)location_id。表单显示,值可选,params在url中传递。但是,即使有东西可以匹配,我提交时也不会显示任何内容。我检查了我的活动管理员,并且有一些地方可以匹配搜索参数。

这是我的 places_controller.rb

def index
   @places = Place.search(params[:kind],[:location_id])
end

我的 place.rb

def self.search(kind, location_id)
  return scoped unless kind.present? || location_id.present?
  where(['kind = ? AND location_id = ?', kind, location_id])
end

和我的搜索表单 _home.html.erb

<%= form_tag places_path, :method => 'get' do %>
    <form class="form-inline text-center" role="form" action="/places">
        <div class="form-group">
           <%= label_tag :kind %>
           <%= select_tag :kind, options_for_select([['beer','0'],['chocolate','1'],['cocktail','2'], ['coffee','3'], ['tea','4'], ['wine','5'], ['juice','6']]), class: "form-control" %>
         /div>
 <br>
         <div class="form-group">
            <%= label_tag :location_id %><br>
            <%= select_tag :location_id, options_for_select([['Houston','0'],['San Francisco','1'],['Santiago','2'], ['Valparaiso','3'], ['Rio de Janeiro','4'], ['Milan','5'], ['Palo Alto','6'], ['Las Vegas','7'], ['New York','8'], ['San Diego','9']]), class: "form-control" %>
          </div>
 <br>
 <br>
          <div class="actions">
          <span itemprop="significantLink">
          <%= submit_tag 'Search', :name => nil, class: "btn btn-default" %>
          </span>
          </div>
 </form>
 <% end %>

非常感谢任何帮助!看了一堆SO / Railscasts并且不确定该怎么做!

1 个答案:

答案 0 :(得分:1)

首先,我看到了一些错误。将索引定义为:

def index
   @places = Place.search(params[:kind], params[:location_id])
end

此外,您可能希望实际使用respond_to块来协商响应应该是什么,例如html或json。

respond_to do |format|
  format.html { YOUR STUFF HERE }
  format.json { YOUR STUFF HERE }
end

我最好的建议是在@places分配之后放置一个debugger语句,以便您可以实际调试正在进行的操作并查看发生了什么。