【问题标题】:Routing a simple search form路由一个简单的搜索表单
【发布时间】:2017-10-09 09:31:33
【问题描述】:

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

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

我已经创建了我的“结果”路线,如下所示:

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>

我错过了什么?

【问题讨论】:

  • 尝试用这个得到“结果”,到:“home#results”

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


【解决方案1】:

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

【讨论】:

    最近更新 更多