Hello and thanks in advance, I´m creating a search engine using ruby on rails. so when the user puts data in the search box, the data is pushed to a query table. right now I´m using turbo frame request so you don't need to press the search button to save data on the query column, this is my issue:
the user looks for the current query: "how can you learn ruby?" but when you take too much time to type, the query column is going to save how can, in consequence, assuming that you have a search name: where is your father's house?, in the query column if you type slow, it is going to appear, where, where is, where are you, etc. what I want is to compare the complete query and erase the others that are incomplete.
this is my search box controller
if params[:query].present?
#articles = Article.where("title LIKE ?", "%#{params[:query]}%")
Search.create(query: params[:query], user_id: current_user.id)
save_search(params[:query], actual_user[:user_id])
else
#articles = Article.all
end
if turbo_frame_request?
render partial: "articles", locals: { articles: #articles}
else
render :index
end
this is my search box on views
<%= form_with(url: articles_path, method: :get, data: {
controller: "search-form", turbo_frame: "articles", turbo_action: "advance"}) do |form| %>
<%= form.label :query, "Search by article" %>
<div>
<%= form.text_field :query, data: { action: "input->search-form#search" } %>
<%= form.submit "Search" %>
so basically i want the code to compare the data on the query table, and erase the incomplete search
Related
I am trying to implement a simple search feature on my site. Currently, when I use my search bar it will work, but if the result is not found then it will pull up a blank page. I am using PostgreSQL
this is my form in recipes/index.html.erb
<%= form_with(url: recipes_path, method: :get, local: true) do |form| %>
<%= form.text_field(:term, {class: 'form-control', type:'search',
arialabel:'search', placeholder: 'search by recipe name'})%>
<%= form.submit 'Search', class: 'btn btn-dark bg-dark' %>
<% end %>
this is my controller index action in the recipes_controller.rb
def index
#recipes = if params[:term]
Recipe.where('name LIKE ?', "%#{params[:term]}")
else
Recipe.all
end
#categories = Category.all
end
I would like to redirect the user back to to the index view and display a message that says something like "the recipe was not found, please search for a different recipe."
In your Index file,
<%= form_tag('/recepies/', :method => :get, :role => 'form') do %>
<%= text_field_tag :term, :class => "form-control", :placeholder => "Search"%>
<button class='glyphicon glyphicon-search' type="search">
</button>
<% end %>
In your controller,
def index
if params[:term].present?
#recipes = Recepie.all
#recipes = #recipes.find_term(params[:term].strip) if params[:term].present?
if #recipes.blank? {
flash[:notice] = 'the recipe was not found, please search for a different recipe.'
}
end
else
#recipes = Recepie.all
end
end
In your Model,
def self.find_term(term)
recipes = all
recipes = posts.where("recipes.term= ?", term)
end
when I use my search bar it will work, but if the result is not found
then it will pull up a blank page.
So your search works but it has no results.
I would like to redirect the user back to to the index view and
display a message that says something like "the recipe was not found,
please search for a different recipe."
You could do something like #recipes.any? to check if you got some results. If you check in the controller you can redirect and use the flash for messages, or you can simply do it in the view and render the message.
PS: Not that you ask, but I really like to use ransack for complex search functionality.
I have a controller that manages a list of Post and I added filter in it. The posts have rating system (0-5 rating). The filter lets user input what rating they want to see, and it will display inputted rating and higher.
For example, I have 10 posts, and 7 of them has rating of 3.5 and higher. If user inputs 3.5 in the rating filter, it will only displays 7 posts with the corresponding rating.
Here is the code:
index.html.erb
...
<%= form_tag posts_path, method: :get, class: "form-inline" do %> <!-- form_tag on posts path (index), use get method since we are displaying results and not altering/ submitting new data -->
<div class="rating form-group">
<%= label_tag :rating %>
<%= number_field_tag :rating, params[:rating], step: 0.5, min: 0, max: 5, class: "form-control" %>
<%= submit_tag "Search", name: nil, class: "btn btn-primary"%>
</div>
<% end %>
...
Here is my index:
def index
#posts = Post.all
#posts = Post.where("rating >= ?", params["rating"]) if params["rating"].present?
end
My problem is, I want to give users the ease of removing the filter whenever they do page refresh. How can I remove the filter every time user refreshes the page so that it displays all 10 posts again? Right now, if I want to display all 10 posts again after making the filter, I either have to go to the field box and delete the number 3.5 I inputted earlier and resubmit the form, or I have to change the url manually from
http://localhost:3000/posts?utf8...&rating=3 to http://localhost:3000/posts; I think it would be more convenient for users to just refresh the page.
I guess I can rephrase it as, how can I change my controller from #posts = Post.where... to #posts = Post.all again when user refreshes page?
I don't know what you routes are, but lets assume your posts_path is posts/index and routes to the index method in the PostsController.
First, I would change your routes config so that post/index will be matched to the index method in the PostsController for both :get and :post requests:
# config/routes.rb
match 'posts/index' => 'posts#index', as: 'posts', via: [:get, :post]
Next, I would change your form to use the :post method instead of :get:
<%= form_tag posts_path, method: :post, class: "form-inline" do %>
<div class="rating form-group">
<%= label_tag :rating %>
<%= number_field_tag :rating, params[:rating],
step: 0.5, min: 0, max: 5, class: "form-control" %>
<%= submit_tag "Search", name: nil, class: "btn btn-primary"%>
</div>
<% end %>
Finally, I would change the where clause your controller to accept params[:rating] if provided, but default to 0 if params[:rating] is nil:
def index
#posts = Post.where("rating >= ?", params[:rating]||=0)
end
This is of course assuming that you do not have negatively rated posts...if you do, you can default to a sufficiently negative number (that all posts are rated higher than).
Now, when the user submits the form, the url will no longer haveparams[:rating] in the query string.
Hope this helps!
The issue here is your query string stays when you refresh the page.
So one way to do it is to replace the url and remove the query string. With the history APi in modern browsers, you can do this.
history.replaceState({}, 'some title', '/');
You can simply plug this into your document / head ready callback. So after your user gets the response with ratings, you change the url back to the one without query string. So when it gets refreshed, it makes request to your server without the rating query string.
But keep in mind, your user won't be able to bookmark your page based of the ratings. If you want to do that, you will need to switch your get requst to post request. Check this wiki page out for more info.
https://en.wikipedia.org/wiki/Post/Redirect/Get
I want to be able to search and update the index.
this is my controller method:
def index
if params[:search]
#ofertas = Oferta.search(params[:search]).paginate(page: params[:ofertas_page], :per_page => 5)
else
#ofertas = Oferta.all.paginate(page: params[:ofertas_page], :per_page => 5)
end
end
My search method in the model
def self.search(search)
where("titulo like ?","%w{search}%")
end
and this is the search form
<%= form_tag ofertas_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search], placeholder: "Procurar Entidades" %>
<%= submit_tag "Procurar", :name => nil %>
</p>
<% end %>
I've seen this setup in a number of search tutorials but no matter what I type nothing appears. Does someone know what I'm doing wrong?
It looks like you were trying to interpolate the search variable into the string, but didn't quite get the right symbol. How about this:
"%#{search}%"
Note the # instead of the w.
where("titulo like ?","%w{search}%")
should be:
where("titulo like ?", "%#{search}%")
#{xxx} is for string interpolation - it allows you to inject ruby (including variables) into a string.
"%xxxx%" is telling SQL that the search string can appear anywhere in the titulo column. '%' is a wildcard in SQL.
%w{xxx yyy zzz} is shorthand for ["xxx", "yyy", "zzz"] - an array in Ruby, which wouldn't mean much to the SQL as a string by itself.
I'm using sunspot for the first time and i'm trying to setup the search. full text search seems to work fine. however, i have a form with a search box and multiple filters on boolean fields that the user can select. somehow the search box works fine but solr isn't picking up the individual booleans as additional filters. also, when i don't do any search text and just want to search by the boolean fields, nothing happens. any help would be appreciated:
this is my controller:
#search = Project.search do
fulltext params[:search]
facet(:master_bedroom)
facet(:dining_room)
facet(:bath)
with(:master_bedroom, params[:mb]) if params[:mb].present?
with(:dining_room, params[:dr]) if params[:dr].present?
with(:bath, params[:p_bath]) if params[:p_bath].present?
end
i have the fields in the model:
searchable do
text :description
boolean :dining_room
boolean :bath
boolean :master_bedroom
end
and i have the following for my view:
<%= form_tag projects_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= check_box_tag :bath, 'true'%>
<%= submit_tag "Search", :name => nil %>
<% end %>
There was an error in variable naming.
I have a search field and button. When the user searches something like "cat" my view renders a bunch of related keywords such as "catnip", "cat toys", "cats" etc... each of these results are links, and are to pass the value of the string displayed back into the search to generate results for the selected link. For example:
User renders search page and searches for "cat"
view page renders results related to "cat" such as "catnip" "kittens"
User now clicks on "catnip"
View page renders results related to "catnip" such as "grass"
Is this possible with link_to? I'm lost and not quite sure what to do...
--
My Code:
SEARCH VIEW PAGE
<% form for(:search, url => search_path) do |f| %>
Search: <%= f.text_field :search %><br>
<%= f.submit "Search Keyword" %><p>
<% unless #keywords.nil? %>
<h3>Keyword Results</h3>
<% #keywords.each do |k| %>
<%= link_to k.name, :controller => "search", :action => "keywords", value => k.name %>
<% end %>
SEARCH CONTROLLER
def keywords
if request.post? then
#keywords = googlesearchapi(params[:search])
end
I want to pass the link_to value that the user clicks on as the :search parameter... thanks in advance for any advice~~
First of all, you want the link to be: "../search/keywords?search=catnip", to do this, modify the link_to as:
<%= link_to k.name, :controller => "search", :action => "keywords", :search => k.name %>
Then, you need to delete the line if request.post? then, otherwise it won't handle the requests coming from link_to, as it is a GET request (not POST).