So this is very weird. I followed this railscast http://railscasts.com/episodes/37-simple-search-form and after I implemented everything it looked like this
index.html.erb
<%= form_tag findjobs_path, :method => 'get' do %>
<p>
<%= text_field_tag :search %>
<%= submit_tag "search" %>
</p>
<% end %>
listings_controller.rb
def index
@listings = Listing.all
@listings = Listing.paginate(:page => params[:page], :per_page => 10)
@user = User.find_by_name(params[:name])
@listing = Listing.find_by_id(params[:id])
@categories = Category.all
@listings = Listing.search(params[:search])
end
end
listing.rb
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
I get the following error: Couldn't find Listing with 'id'=all I understand that the find methods looks right away for the id. I however don't know how I need to set it so that it searches through all my listings. find_by_all of course does not work.
I hope anyone can help
Thank you so muc
how about modify listing.rb method? 方法?
def self.search(search)
if search
self.where("name like ?", "%#{search}%")
else
self.all
end
end
Plus..
listings_controller.rb
def index
@listings = Listing.all
# Patching all Listing
@listing = Listing.where(id: params[:id]) if params[:id].present?
# Find By Id (For pagination, the 'where' statement result is Listing ActiveRecord::Relationship )
@listings = @listings.search(params[:search]) if params[:search].present?
# Search using Keyword
@listings = @listings.paginate(:page => params[:page], :per_page => 10)
# Pagination
@user = User.find_by_name(params[:name]) if params[:name].present?
# Find User using name column
@categories = Category.all
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.