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
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")) %>");
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.
What I am trying to do is add a simple search to a 'show' view in my rails app. I've watched the rails cast about simple search and it's given me some good direction but I can't seem to get it to work with my situation. I just have a list of FAQs and want to have a search filter for the 'question(s)'.
Here's my views/guests/show.html.erb
<%= form_tag faq_search_guests_path, method: :get, remote: true, :id => "faq_search_form" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<div id="faq-search-test"></div>
<% #faqs.each do |f| %>
<h4>Question</h4>
<%= f.question %>
<h4>Answer</h4>
<%= f.answer %>
<% end %>
Here's the controller/guests_controller.rb method that originally sets #faqs
def show
#guest = Guest.find(params[:id])
#hotel = Hotel.find(params[:hotel_id])
#faqs = Faq.where(hotel_id: #hotel.id)
#template = Template.where( hotel: #hotel.id )
#visit = Visit.where("hotel_id = ? AND guest_id = ?", #hotel.id, #guest.id).last
#visit = Visit.find(params[:visit_id]) if params[:visit_id] && (#visit.hotel_id == #hotel.id)
unless current_user.hotels.include?(#hotel)
render :file => "public/401.html", :status => :unauthorized
end
end
Here's my controllers/guests_controller.rb custom method my form is calling
def faq_search
#faq_result = Faq.search(params[:search])
render :nothing => true
end
Here's my routes.rb for that
resources :guests do
get 'faq_search', :on => :collection
end
resources :faqs
Here's my models/faq.rb
def self.search(search)
if search
where('question LIKE ?', '%#{search}%')
else
scoped
end
end
So from here, I am having trouble displaying #faq_result in show.html.erb. I guess I have no idea how to access an instance variable from a custom controller method from a view.
Any help would be much appreiciated :)!
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!!!