Ruby on Rails 4: Display Search Results on the Search Results Page

I am currently implementing search functions in a project, and I am struggling to display it on a special search results page.

Knowing the questions on this topic already, but not being able to work out a solution due to complete incompetence, I ask you for the final index :).

The search form appears on the index page, which is entry_path and root_path. I would like to pass the parameters to a new page, search_path.

Here are my files:

Entriescontroller

def search
end

def index
  @entries = Entry.all.order('entries.created_at DESC')
  @entry = Entry.new # My index page also creates new entries.

  if params[:search]
    @entries = Entry.search(params[:search]).order("created_at DESC")
  else
    @entries = Entry.all.order("created_at DESC")
  end

Model: entry.rb

def self.search(search)
  where("content LIKE ? OR created_at LIKE ?", "%#{search}%", "%#{search}%") 
end

routes.rb

Rails.application.routes.draw do
  resources :entries 
  root                  'entries#index'  
  get   'new'       =>  'entries/new'
  get   'show'      =>  'entries/show'
  get   'edit'      =>  'entries/edit'
  get   'search'    =>  'entries/search'

Finally: form in index

<%= form_tag(entries_path, :method => "get", class: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search for previous entries..", class: "form-control" %>
<% end %>

When I change path_path_path to search_path, I get the message "We're sorry, but something went wrong. If you are the owner of the application, check the logs for more information." Therefore, I suspect that this is a routing problem. However, I cannot figure it out. The magazine says:

ActionController::RoutingError (uninitialized constant Entries):
Few, I would like to know what is happening here! Thanks for the bunch.
0
source share
2 answers

Modify routes.rb

Rails.application.routes.draw do
  root                  'entries#index'
  resources :entries  do
    collection do
      get :search
    end
  end
end

change the form pathin searchon the index page :

<%= form_tag(search_entries_path, :method => :get, class: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search for previous entries..", class: "form-control" %>
<% end %>

Change your controller method:

def search
  if params[:search]
    @entries = Entry.search(params[:search]).order("created_at DESC")
  else
    @entries = Entry.all.order("created_at DESC")
  end
end

create one template for the search method in Here you can access the object view/entries/search.html.erb @entries


Points of change I made:

1. routes.rb:

Rails URL- . paths URLs, . resource . HTTP verbs URLs . CRUD .

, collection individual members of the collection.

:

, - :

resources :entries do
  member do
    get 'preview'
  end
end

:

resources :entries do
  collection do
    get 'search'
  end
end

A ID, member. , collection objects. ?

2. GET POST?

, GET POST , SO. GET POST , -, . , , , GET. .;)

. GET ( - ), POST ( / ), PUT ( PATCH, ), , DELETE, .

:

, . :)

+1

html Index.

<form>
  <legend>Search</legend>
  <div class='col-xs-4'>
    <input type='text' class='form-control' value='<%= params[:search] %>' name='keyword' placeholder='Keyword' >
  </div>
</form>

. . search methods,

0

Source: https://habr.com/ru/post/1680945/


All Articles