您如何将搜索添加到布局中,以便它可以通过网站的每个页面搜索帖子?比如在stackoverflow上。
教程展示了将搜索方法添加到 PostsController 的 index 操作,然后在 views/post/index.html.erb 中添加表单和结果块。
我一直在尝试在 application.html.erb 中创建一个表单,该表单将获取请求发送到帖子控制器的搜索操作。我似乎无法正确解决,有人可以帮助解释我哪里出错了吗?
目前我在访问我的主页时收到此错误:
页面中的名称错误#home
未定义的局部变量或方法“search_posts_path”
帖子控制器
def search
if params[:q]
query = params[:q]
@search = Post.search do
keywords query
end
@posts = @search.results
end
end
后模型
searchable do
text :title, :default_boost => 2
text :content
end
路线.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
视图/布局/application.html.erb
<%= form_tag search_posts_path, :method => :get do %>
<p>
<%= text_field_tag :q, params[:q] %> <%= submit_tag "Search!" %>
</p>
<% end %>
页面控制器
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