【发布时间】:2016-04-12 20:41:10
【问题描述】:
我环顾四周,找不到我的解决方案。
我正在我的 rails 应用程序中创建跨多个模型的搜索。现在,我设置了两个模型,一个用户和一个社区。当我转到“/search”然后在导航栏中执行搜索时,我的搜索工作正常。
我的问题是,当用户尝试在导航的主搜索栏中搜索某些内容时,它不会重定向或转发到“/search”。它只是转发到以下链接:
http://localhost:3000/?utf8=%E2%9C%93&search=&commit=Search
但是,我真的需要它去:
http://localhost:3000/search?utf8=%E2%9C%93&search=&commit=Search
为了重定向并完成搜索。
routes.rb
get '/search', to: 'search#search'
搜索控制器
class SearchController < ApplicationController
def search
@search = Sunspot.search Community,User do
fulltext params[:search]
end
@results = @search.results
end
end
community.rb
class Community < ActiveRecord::Base
searchable do
text :name
text :description, :expanded_description
end
end
user.rb
class User < ActiveRecord::Base
searchable do
text :username
text :name
end
end
标题视图(在haml中)
%form.navbar-form.navbar-left
= form_tag search_path, method: 'get' do
= text_field_tag :search, params[:search], class: 'form-control'
= submit_tag "Search", class: 'btn btn-default'
非常感谢提前,或者如果有其他类似的问题,请指路。
以下是我搜索时发生的日志。
Started GET "/?utf8=%E2%9C%93&search=new&commit=Search" for ::1 at 2016-04-12 17:01:52 -0400
Processing by HomeController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"new", "commit"=>"Search"}
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY created_at DESC LIMIT 1
Membership Load (0.4ms) SELECT `memberships`.* FROM `memberships` WHERE `memberships`.`user_id` = 1
Rendered home/_communities_blank_state.html.haml (0.6ms)
Rendered shared/_footer_ctas.html.haml (0.7ms)
Rendered home/_dashboard.html.haml (4.5ms)
Rendered home/index.html.haml within layouts/application (8.2ms)
Rendered shared/_alerts.html.haml (0.2ms)
Rendered shared/_header.html.haml (1.6ms)
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
devise_for :users, :controllers => { :registrations => 'users' }
devise_scope :users do
resources :profiles, controller: "profile"
end
root to: 'home#index'
get 'portal', to: 'home#portal'
get '/search', to: 'search#search', as: :search
resources :communities do
resources :supplements
end
resources :contacts, only: [:new, :create]
get '/contact_us' => 'contacts#new'
mount Ckeditor::Engine => '/ckeditor'
end
【问题讨论】:
标签: ruby-on-rails search solr