路由一个简单的搜索表单

Routing a simple search form

我正在尝试创建一个简单的搜索表单,将用户转到单独的 'results' 页面。

我对 index.html.erb 的看法如下:

<%= simple_form_for results_path, method: :get, html: { class: "form-inline justify-content-center" } do |f| %>

    <%= f.select :what, options_for_select([['Happy Hours Anywhere','Anywhere'],['After Work Drinks','After Work Drinks'],['Somewhere to Relax with Friends', 'Relaxing with Friends'], ['A Club Night','Club Night'], ['Somewhere for Date Night','Date Night'], ['A Place to Watch Sports', 'Watching Sports']]),{} ,:class => "form-control select-box font-lightweight"  %>

    <%= f.select :datetime, options_for_select(dates_for_search_select.each_with_index.map{|d, i| [d[1],d[0]]}), {}, :class => "form-control select-box font-lightweight" %>

    <%= f.select :time, options_for_select(times_for_search_select.each_with_index.map{|d, i| [d[1],d[0]]}), {}, :class => "form-control select-box font-lightweight" %>

    <%= f.button :submit, 'Discover', :class => 'btn btn-block btn-danger btn-embossed top-margin ' %>
<%end%>

我已经创建了我的 'results' 路线,如下所示:

Rails.application.routes.draw do
  resources :offers
  resources :venues
    get 'home/results' => 'home#results', :as => :results
    get 'home/index'
  devise_for :users
  root to: "home#index"    
end

然而,当我生成 HTML 生成的表单时,操作会一直路由回索引页面:

<form class="simple_form form-inline justify-content-center" novalidate="novalidate" 
 action="/home/index" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">

</form>

我错过了什么?

simple_form_for 要求一个对象作为第一个参数(class 实例或符号)。然后您可以像添加普通 form_for 一样添加 url: articles_path。参见 simple form source code

所以你需要有这样一行:

<%= simple_form_for :results, url: results_path, method: :get, html: { class: "form-inline justify-content-center" } do |f| %>