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 %>
Related
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!!!
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")
I'm trying to send pdf to user. For example if user is in /products?category_id=3 he has to get report of products whose category id is 3 not all reports. I couldn't find a way to pass parameter. How can i do this ?
products_controller
def index
#products Product.where("category_id = ?", params[:category_id)
end
def reporter
kit = PDFKit.new(render_to_string(:action => "products/index", :layout => "report"))
send_data(kit.to_pdf, :type => :pdf)
end
My view is like this
<% #products.each do |product| %>
<%= product.name %>
<% end %>
<%= link_to "Download(PDF)", reporter_path, :method => :post %>