我已經實現了一個簡單的導軌搜索,它正在搜索我的數據庫並顯示結果很好。但是,我不完全瞭解如何將我的搜索查詢路由到新頁面。路由導軌簡單搜索到列表頁面
我已經用護欄腳手架創建我的網站的這一特定部分:
class DogSearchesController < ApplicationController
before_action :set_dog_search, only: [:show, :edit, :update, :destroy]
# GET /dog_searches
# GET /dog_searches.json
def index
@dog_searches = DogSearch.where(["dog LIKE ? ","%#{params[:search]}%"])
end
# GET /dog_searches/1
# GET /dog_searches/1.json
def show
@dog_search = DogSearch.find(params[:city])
end
index.html.rb
<%= form_tag dog_searches_path, :method => 'get', :class => 'search-form' do %>
<i class="fa fa-map-marker"></i>
<%= text_field_tag :search, params[:search], :class => 'homesearch', :placeholder => 'Enter search term' %>
<%= my_button_to '<i class="fa fa-search"></i>', {}, :class => "fa fa-search" %>
<% end %>
我在dog_searches/listing
創建一個頁面,並將此向我的路線文件爲get 'dog_searches/listing'
任何幫助,將不勝感激。
這很棒,效果很好。如果這是常見做法,我認爲我會考慮使用索引,因爲它沒有意義偏離。當搜索運行時,頁面設計將有所不同,這就是爲什麼我在查看與索引分開的列表。仍在學習使用Rails進行路由的過程,這讓我非常困惑。 – JustDanLee
有些時候,您想要獲得相同結果的不同視圖 - 例如,在索引頁面上使用csv或excel版本很常見。我可以看到其他用途。您可以將兩個頁面視爲列表 - 只要鏈接到另一個頁面,方法是在索引頁面呈現後,通過更改操作並保留其他參數 - 即link_to(params.merge(:action =>:listing))' 。最後,如果需要,您可以從索引頁面重定向到列表頁面。該選項並不常見。相反,你可能想要有一個'search_form'頁面並提交給索引。 – Swards