【发布时间】:2014-08-09 02:04:23
【问题描述】:
我正在尝试在我的 Rails 应用程序上进行简单的搜索。我只有两个选择器可供选择:(a) kind 和 (b) location_id。表单显示,值可选,参数在 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/Railscast,但不知道该怎么做!
【问题讨论】:
-
在您的代码中,您在哪里使用您为搜索结果设置的
@places变量? -
在我的 index.html.erb /places
标签: ruby-on-rails ruby search query-parameters