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!!!
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 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.
I am working on rails app , In which I have created a table Product Name:string and Number: integer.
The application should give user a form where he can search a product by his number if product exists it should give product name from database.
My search.html.erb is this.
<%= form_for #products, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_area :number, :size => "60x12" %>
<%= f.submit "Search" %>
<% end
What will be the definition of search Method in ProductController and routes i need to add in routes.rb?
Irrespective of nifty forms, this is how I would have done this:
In config/routes.rb
replace resources :products' with
resources :products do
post 'search', :on => :collection
end
This will give me a search_products_path
In your view:
<%= form_for(:search, :url => search_products_path) do |f| %>
<%= f.text_field :number, :placeholder => "Enter number to search" %>
<%= f.submit "Search" %>
<% end %>
In your products_controller.rb
def search
number = params[:search][:number]
#result = Product.find_by_number(number)
#not_found = true unless #result
end
In your views/products/search.html.erb, use #result to show the product information; take care while checking whether or not the desired product is actually found or not. I have set the boolean #not_found in case it doesn't exist.
Hi I have a search bar for my Firms.. however it seems to be throwing up and error and I cannot work out why!
My View
application.html.erb
<%= form_tag firms_path, :method => 'get', :class => 'form-search' do %>
<%= text_field_tag :search, params[:search], :class => 'input-medium search-query', :placeholder => 'Firm name' %>
<%= submit_tag "Search", :name => nil ,:class => 'btn' %>
<% end %>
My firms_controller.rb
def index
#firms = Firm.search(params[:search])
end
def self.search(search)
if search
where(['name LIKE ?', "%#{search}%"]).page(params[:id])
else
scoped
end
end
This used to work fine, but now it is tossing up this error.
NameError in FirmsController#index
undefined local variable or method `params' for #<Class:0x007f93cd8b2ac8>
app/models/firm.rb:13:in `search'
app/controllers/firms_controller.rb:8:in `index'
Any ideas?
Thanks in advance for any help
Ross
Modify
def index
#firms = Firm.search(params[:search], params[:id])
end
def self.search(search, id)
if search
where(['name LIKE ?', "%#{search}%"]).page(id)
else
scoped
end
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")