路由一个简单的搜索表单

时间:2017-10-09 09:31:33

标签: ruby-on-rails forms routes ruby-on-rails-5 simple-form

我试图创建一个简单的搜索表单,将用户转移到单独的结果'页。

我对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%>

我创造了我的结果&#39;路线,可以在下面看到:

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>

我缺少什么?

1 个答案:

答案 0 :(得分:0)

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

所以你需要这样的一行:

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