Rails 3:sunspot solr:为每个页面添加搜索功能

发布于 2024-11-07 11:01:10 字数 2063 浏览 5 评论 0原文

如何将搜索添加到布局中,以便它可以在网站的每个页面上搜索帖子?比如 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-11-14 11:01:10

这是一个 Ruby on Rails 路由问题。

member 路由用于对单个记录进行操作。因此,您正在定义 search_post_path(@post) ,它将路由到类似 /posts/1/search 的内容,

您想要的是 collection 路由。

resources :posts do
  member do
    get :likers
  end
  collection do
    get :search
  end
end

这将创建 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 defining search_post_path(@post) which will route to something like /posts/1/search

What you want is a collection route.

resources :posts do
  member do
    get :likers
  end
  collection do
    get :search
  end
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文