Rails 3:sunspot solr:为每个页面添加搜索功能
如何将搜索添加到布局中,以便它可以在网站的每个页面上搜索帖子?比如 stackoverflow 上的这里。
教程展示了将搜索方法添加到 PostsController 的索引操作,然后在views/post/index.html.erb 中添加表单和结果块。
我一直在尝试在 application.html.erb 中创建一个表单,将 get 请求发送到 posts 控制器的搜索操作。我似乎无法理解,有人可以帮助解释我哪里出错了吗?
目前,我在访问主页时遇到此错误:
NameError in Pages#home
undefined local variable or method `search_posts_path'
PostsController
def search
if params[:q]
query = params[:q]
@search = Post.search do
keywords query
end
@posts = @search.results
end
end
post model
searchable do
text :title, :default_boost => 2
text :content
end
paths.rb
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :users, :controllers => {:registrations => 'registrations'}
resources :posts do
member do
get :likers, :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
resources :collections
end
views/layouts/application.html.erb
<%= form_tag search_posts_path, :method => :get do %>
<p>
<%= text_field_tag :q, params[:q] %> <%= submit_tag "Search!" %>
</p>
<% end %>
PagesController
def home
@title = "Home"
if user_signed_in?
@user = current_user
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
else
#render :layout => 'special_layout'
end
end
How do you add search to a layout so it can search for posts through every page of the site? such as here on stackoverflow.
Tutorials show adding the search method to the index action of the PostsController, and then adding the form and results block in views/post/index.html.erb.
I've been trying to create a form in application.html.erb that sends a get request to the search action of the posts controller. I can't seem to get it right, can someone help explain where I'm going wrong?
Currently I'm getting this error when going to my homepage:
NameError in Pages#home
undefined local variable or method `search_posts_path'
PostsController
def search
if params[:q]
query = params[:q]
@search = Post.search do
keywords query
end
@posts = @search.results
end
end
post model
searchable do
text :title, :default_boost => 2
text :content
end
routes.rb
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :users, :controllers => {:registrations => 'registrations'}
resources :posts do
member do
get :likers, :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
resources :collections
end
views/layouts/application.html.erb
<%= form_tag search_posts_path, :method => :get do %>
<p>
<%= text_field_tag :q, params[:q] %> <%= submit_tag "Search!" %>
</p>
<% end %>
PagesController
def home
@title = "Home"
if user_signed_in?
@user = current_user
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
else
#render :layout => 'special_layout'
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 Ruby on Rails 路由问题。
member
路由用于对单个记录进行操作。因此,您正在定义search_post_path(@post)
,它将路由到类似/posts/1/search
的内容,您想要的是
collection
路由。这将创建
search_posts_path
方法并按照您的预期路由到/posts/search
。另请参阅:http://guides.rubyonrails.org/routing.html#添加更多restful-actions
This is a Ruby on Rails routing question.
The
member
route is for operating on a single record. So you are definingsearch_post_path(@post)
which will route to something like/posts/1/search
What you want is a
collection
route.This will create the
search_posts_path
method and route to/posts/search
as you are expecting.See also: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions