I had install will_paginate and in the posts_controller index def write:
#posts = Post.paginate(:page => params[:page], :per_page => 10, :order => 'id DESC', :conditions => ['title like ?', "%#{params[:search]}%"])
But search doesn't work. When I click on search button it changes url to:
http://178.62.xxx.xxx/find/?utf8=%E2%9C%93&search=the
And the posts in the page stay the same.
Routes:
get "/find/" => "posts#index"
Post model:
def self.search(query)
where("title like :title", title: "%#{query.strip.chomp}%")
end
In the index.html.erb:
<%= form_tag('/find/', :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Найти посты", :class => "search" %>
<%= submit_tag "OK", :name => nil, :class => "search_btn" %>
<% end %>
I solved it.
First, I created search form which request to index action (GET /posts).
# index.html.erb - Search Form
<%= form_tag posts_path, method: :get do %>
<%= text_field_tag :search, "" %>
<%= submit_tag 'Search' %>
<% end %>
Next, I defined index action.
# Controller
def index
# params = { :search => 'search keywords ...' }
#products = Product.search(params).paginate(:page => params[:page], :per_page => 2)
end
Then, define Post#search.
paginate method is used on only ActiveRecord::Relation.
So, Post#search must return ActiveRecord::Relation.
def self.search(params)
products = all # for not existing params args
products = products.where("name like ?", "%#{params[:search]}%") if params[:search]
products
end
That's all.
Related
I'm using a gem called gem 'open_taobao'
The following is how i get my data in json
class SearchController < ApplicationController
layout 'layouts/frontend_base'
def index
#searchterm = "basketball"
#search = OpenTaobao.get(
:method => "taobao.tbk.item.get",
:fields => "num_iid,title,nick,pict_url,cid,price,type,delist_time,post_fee,score,volume",
:q => #searchterm,
:page_size => 1,
:sort => "_des",
)
end
end
What I am trying to do from my view is have a input field with submit and whatever is in the input box will replace my query variable below
:q => #searchterm
What would be the best way of accomplishing this?
You can have your form like this:
<%= form_tag search_path do %>
<%= input_field_tag :q, params[:q] %>
<%= submit_tag "Submit" %>
<% end %>
in your controller, you shouldn't need to set another instance variable #searchterm
You could do:
def index
#search = OpenTaobao.get(
:method => "taobao.tbk.item.get",
:fields => "num_iid,title,nick,pict_url,cid,price,type,delist_time,post_fee,score,volume",
:q => params[:q],
:page_size => 1,
:sort => "_des",
)
# what you do depends on the data type returned here. if it's a JSON string, you should parse it and reassign to #search, if not you may not need to do anything
end
In your view,
#search/index.html.erb
<% #search.each do |result_key, result_value| %>
# you can deal with the html here
<%= result_key %>
<%= result_value %>
<% end %>
I have a requirement to create a filter in the index page, I do not have much idea on jQuery/AJAX, please help me in creating the code
// index view
- #results.each do |result|
%tr
%td= result.test.name
%td= result.status
Here is my controller
def index
#results = Result.all
end
I think you are searching for "SEARCH USING JQUERY".
There is a good tutorial here
First you must create your controller like this:
def index
#products = Product.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
Then your model:
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
Your index view:
<% form_tag products_path, :method => 'get', :id => "products_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="products"><%= render 'products' %></div>
<% end %>
Javascript view:
$("#products").html("<%= escape_javascript(render("products")) %>");
Listing model code
def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['franchisename LIKE ? or longdescription LIKE ?', search_condition, search_condition])
end
Home controller
def search
#results = Listing.search params[:search]
end
Route
get 'home/search'
Home/index.html.erb search form
<%= form_tag :controller => 'home', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'indexsearch' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Home/search.html.erb search results
<%= #results.each do |result| %>
<%= result.franchisename %>
<%= result.shortdescription %>
<% end %>
Error: No route matches [POST] "/home/search"
I just want it so that I can type in my search query on the home page and then upon submission it redirects to a results page containing the results. I followed a guide initially but maybe it was outdated, though it doesn't say. Would appreciate some help!
Just defined the form_tag in the following way.
<%= form_tag url_for(:controller => 'home', :action => 'search'), :method => 'get' do %>
Hope your problem will be resolved!
Probably you should specify controller and action in to your routes like below:
get 'home/search' => 'home#search'
Hope this resolve your problem!!!
Cheers!!!
I have a search bar partially working. However it seems that from my home page it does not redirect to my products#index page. When I search for a product I get the following url: http://localhost:3000/?utf8=%E2%9C%93&search=intel however if I change the url to look like the following: http://localhost:3000/search?utf8=%E2%9C%93&search=intel this will work. My setup follows:
SearchController
class SearchController < ApplicationController
def index
#products = Product.all(:conditions => ['title LIKE ?', "%#{params[:search]}%"])
end
end
ProductsController.rb
def index
#products = Product.filter(params[:search], [:title])
respond_to do |format|
format.html
format.json { render json: #products }
end
end
application.html.erb
<%= form_tag search_path, :class => 'navbar-form to-the-right', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :class => 'span2', :placeholder => 'Search' %>
<% end %>
</form>
Routes.rb
match '/search' => 'search#index'
I cannot seem to identify why this is not working!
Your route does not provide a helper search_path. To enable that do this:
match '/search' => 'search#index', as: :search
according to your post i try this :
class ProductsController < ApplicationController
def index
#products = Product.filter(params[:title])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
product.rb
def self.filter (search_title)
return scoped unless search_title.present?
where(['title LIKE ?', "%#{search_title}%"])
end
route.rb
match '/search' => 'products#index'
application.html
<%= form_tag search_path, :class => 'navbar-form to-the-right', :method => 'get' do %>
<%= text_field_tag :title, params[:title], :class => 'span2', :placeholder => 'Search' %>
<% end %>
I am using the Crowdint rails3 jquery autocomplete and having trouble with my search form.
This is how my search form looks without autocomplete:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], :placeholder => "Search for a Product.....", :id => "main-search-field" %>
<%= submit_tag "Search", :name => nil, :id => "main-search-field-button" %>
<%end%>
Now when I change around the form for autocomplete and search:
<%= form_tag search_path, :method => 'get' do %>
<%= autocomplete_field_tag 'name','', search_autocomplete_product_name_path, params[:search], :placeholder => "Search for a Product.....", :id => "main-search-field" %>
<%= submit_tag "Search", :name => nil, :id => "main-search-field-button" %>
<%end%>
This will not work if I have params[:search] inside of my autocomplete_field_tag:
ActionView::Template::Error (wrong number of arguments (5 for 4))
How do I set the search parameter so I can actually search with autocomplete?
More info:
class SearchController < ApplicationController
autocomplete :product, :name, :full => true
# Sunspot search.
def index
#search = Product.search do
fulltext params[:search]
paginate(:per_page => 1, :page => params[:page])
end
#products = #search.results
end
end
# routes.rb
get 'search/autocomplete_product_name'
resources :search, :only => [:index]
(Disclaimer: I've no experience with this particular gem, the following answer is based on a brief look through it's source only.)
The autocomplete_field_tag method takes five parameters according to the source, the last one being an options hash:
autocomplete_field_tag(name, value, source, options = {})
So, given that your controller method e.g. needs a parameter called product_name your method call should probably read:
autocomplete_field_tag('product_name', '', search_autocomplete_product_name_path, :placeholder => "Search for a Product.....", :id => "main-search-field")