I have a working search form in my RoR app however it doesn't recognise capital letters. So if i search for something it has to be case sensitive. I tried adding UPPER in the search method however it raises an error. How do I get it not be case sensitive?
Here is the controller:
def index
if params[:search]
#restaurants = Restaurant.search(params[:search]).order("created_at DESC")
else
#restaurants = Restaurant.joins("LEFT JOIN reviews ON restaurants.id = reviews.restaurant_id").group("restaurants.id").order("AVG(reviews.rating) DESC NULLS LAST")
end
end
This is the method from the model:
def self.search(query)
where("name LIKE ?", "%#{query}%")
end
And finally the form:
<section class="search-form">
<%= form_tag(restaurants_path, :method => "get", id: "search-form", class: "navbar-form navbar-left") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search restaurants", :autocomplete => :off, class: "form-control", id: "search-field", required: "required" %>
</div>
<%= submit_tag "Search", :name => nil, id: "search-button", class: "btn btn-default" %>
<% end %>
</section>
Would appreciate any advice. Thank you.
You can always convert params[:search] to lowercase, I'd do something like this:
Restaurant.search(params_search).order("created_at DESC")
private
def params_search
params[:search].to_s.downcase
end
Related
Hey guys I am trying to implement pagination using the will_paginate gem, and it doesn't seem to be working. I have added the gem to my Gemfile and tried the code below:
controller
def team_search
#teams = CareTeam.all.order('created_at ASC').paginate(page:params[:page], per_page: 10)
end
view
<h1>Search for a team</h1>
<p>search for a team by team name</p>
<%= form_tag search_team_results_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], placeholder:
"Search Teams By name", required: true %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= will_paginate #teams %>
model
def self.search(team_name)
if team_name
team_name.downcase!
self.where('LOWER(team_name) LIKE ?', "%#{team_name}%").order('created_at ASC')
else
CareTeam.all.order('created_at ASC')
end
end
When I navigate to my view, nothing shows up. Does anyone analyse from above what could be wrong?
I'm implementing the website. I got a problem for a search form. I upload my code, and what i want to ask is how to set the search 'path' through 'index' and 'historical' on homes_controller
Below, my code:
app/controllers/homes_controller
def index
#homes = Home.where(:category => 1).reverse
end
def historical
#homes = Home.where(:category => 2).reverse
end
app/views/layouts/application.html.erb
Below, this code is temporary code for now. I should change it.
<%= form_tag(homes_path, :method => 'get', id: "search-form" do %>
<%= text_field_tag :search, params[:search], placeholder: "검색" %>
<%= submit_tag "검색", :name => nil %>
<% end %>
Am not sure what you are supposed to do here
But as per the question - I can give a solution to your problem
Keep an instance variable in your controller actions - like this
app/controllers/homes_controller
def index
#homes = Home.where(:category => 1).reverse
#search_path = "path you want to give"
end
def historical
#homes = Home.where(:category => 2).reverse
#search_path = "path you want to give"
end
and in your layout you can use it like this
app/views/layouts/application.html.erb
<%= #search_path.present? %>
<%= form_tag(#search_path, :method => 'get', id: "search-form" do %>
<%= text_field_tag :search, params[:search], placeholder: "검색" %>
<%= submit_tag "검색", :name => nil %>
<% end %>
<% end %>
I'm a total beginner in Rails and I try to use Ransack to filter results.
I have a model called 'adventures' which has many parameters, of which 'main_activity'. I try to implement a form in my homepage, where you can type the kind of activities you are looking for (later I will implement a dropdown so it will be limited to existing activities).
Here is what I have in my pages_controller:
def search
if params[:search].present? && params[:search].strip != ""
session[:bourse_aventuriers_search] = params[:search]
end
arrResult = Array.new
if session[:bourse_aventuriers_search] && session[:bourse_aventuriers_search] != ""
#adventures_main_activity = Adventure.where(active: true).all
else
#adventures_main_activity = Adventure.where(active: true).all
end
#search = #adventures_main_activity.ransack(params[:q])
#adventures = #search.result
#arrAdventures = #adventures.to_a
end
And in my home.html.erb
<%= form_tag search_path, method: :get do %>
<div class="row">
<div class="col-md-6">
<%= text_field_tag :search, params[:search], placeholder: "Quelle activité ?",
class:"form-control" %>
</div>
For the moment, whatever I type in the form, I get all the 'adventures' in the data base ; I find it logical because I did not change the 1st #adventures_main_activity = Adventure.where(active: true).all.
I don't know how to change it so that It will give me only the adventures whose main_activity is the keyword that I type in the form. Can anyone help me ? Thanks :)
I think you have a lot of surplus code in your search. Your controller should only need the #q parameter as follows:
#q = Adventure.ransack(params[:q])
#adventures = #q.result
The #adventures will then return any matches as an active record.
If you pass no parameters then the query will return all records (same as doing an Adventure.all).
When you submit the form the ransack search will pass a "q" param which will contain all the form items. Your's may look something like this:
"utf8"=>"✓", "q"=>{"search_cont"=>"rock climbing"}, "commit"=>"Search", "controller"=>"adventures", "action"=>"index"
If you use a debugger you can see this information by typing "params" in the command line. It will also appear in the server output.
Using an example of one I did in my application here is the exact code I used:
View:
<%= search_form_for #q do |f| %>
<div class="form-group col-sm-6">
<%= f.label :email_cont, "Email:" %>
<%= f.search_field :email_cont, :placeholder => "Please enter and email to search for", :class => "form-control" %>
</div>
<div class="form-group col-sm-3">
<%= f.submit 'Search', :class => 'btn btn-primary' %>
<%= link_to 'Reset', users_path, :class => 'btn btn-default' %>
</div>
<% end %>
Controller
def index
#q = User.ransack(params[:q])
#users = #q.result(distinct: true).order(:email).page params[:page]
end
The classes are just Bootstrap CSS which you won't need if you are not using bootstrap.
EDIT
The search_form_for #q has to exactly that and the #q has to be set in the params for it to work. Ransack is very specific about them. So your #search in
#search = #adventures_main_activity.ransack(params[:q])
should be #q. In your view change your form_tag to search_form_for and it should work. Good luck.
I'm fairly new to rails and am building an online shop just to write some rails. I'm in the process of implementing a simple search function at the moment and get some strange behaviour I can't explain.
Model method:
def self.search(query)
where("title like ?", "%#{query}%")
end
Controller methode:
def index
if params[:search]
#products = Product.search(params[:search])
else
#products = []
#Only lists available products (in cart counts as available)
#available_items = Item.where(user_id: nil).select(:product_id).uniq
#available_items.each do |item|
#products << item.product
end
end
end
Search form:
<%= form_tag(products_path, :method => "get", id: "search-form", enforce_utf8: false) do %>
<%= text_field :search, params[:search], placeholder: "Search..." %>
<%= submit_tag "Search", :name => nil %>
<% end %>
When I trigger a search I get no results and the url looks like this:
http://localhost:3000/products?search%5B%5D=Paper
When I remove '%5B%5D' from the url it all works fine and I get my results. '%5B%5D' stands for '[]' in URI encoding, can't figure out where that come from though. Any help would be greatly appreciated!
Best,
Paul
See reference how text_field and reference for text_field_tag helper works:
<%= text_field :search, params[:search], placeholder: "Search..." %>
It will give you search input field with name=search[], thats why its passing search[]='text'.
<input type="text" name="search[]" placeholder="Search..." />
Use text_field_tag instead:
<%= text_field_tag :search, params[:search], placeholder: "Search..." %>
I'm very new at Ruby on Rails and trying to make a simple search function for my application. The search function keeps returning this ActiveRecord error (here I searched for "london" in "from"):
SQLite3::SQLException: near "from": syntax error: SELECT "journeys".* FROM "journeys" WHERE (from LIKE '%london%')
Model:
class Journey < ActiveRecord::Base
validates :from, presence: true, length: { minimum: 1 }
def self.search(search_from)
self.where("from LIKE ?", "%#{search_from}%")
end
end
Controller:
class JourneysController < ApplicationController
def index
#journeys = Journey.all
if params[:search_from]
#journeys = Journey.search(params[:search_from])
else
#journeys = Journey.all.order('created_at DESC')
end
end
View:
<p>
<%= form_tag(journeys_path, :method => "get", from: "search-form") do %>
<%= text_field_tag :search_from, params[:search_from], placeholder: "Search from", :class => 'input' %>
<%= submit_tag "Search", :class => 'submit' %>
<% end %>
</p>
<% if #journey.present? %>
<%= render #journey %>
<% else %>
<p>There is nobody going from <%= params[:search_from] %>.</p>
<% end %>
Like I said, I'm very new at RoR so this is probably stupid to even ask for, but I would really appreciate the help.
Problem is that from is reserved in SQL. I highly suggest to rename this field, though if you want to use it as is - self.where("\"from\" LIKE ?", "%#{search_from}%").