Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4612798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:27:13+00:00 2026-05-22T01:27:13+00:00

How do you add search to a layout so it can search for posts

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:27:14+00:00Added an answer on May 22, 2026 at 1:27 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add full web search to my site. I need something like
How can I add a database field to the Magento global search? I want
Here is my code for css div based layout. I want a search box
how can I add a var to an activity that start after a search?
How can I add a form to my layout.phtml? I would like to be
How do you add Search form to your Views in Drupal? Do I have
I am building an open search add-on for Firefox/IE and the image needs to
I'm wanting to add a search box onto my website - which is built
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.
I am trying to add full text search capabilities to my RoR app, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.