I'm using the generic search form, and my url after the search looks like
http://localhost:3000/search?commit=Search&page=2&query=feature&utf8=%E2%9C%93
The search works fine, but I would like to remove the default "utf8=✓" and "commit=Search" parameters from the URL, I'm also using will_paginate and I would like the &page=2 to be after the query parameter leaving it like this:
http://localhost:3000/search?query=feature&page=2
My code:
#posts_controller.rb
def search
query = '%'+params[:query]+'%'
#posts = Post.find(:all, :conditions => ["content LIKE ? or title LIKE ?", query, query]).paginate(:page => params[:page], :per_page => 5)
end
and
#html form
<%= form_tag(search_path, :method => 'get') do %>
<%= text_field_tag "query" %>
<%= submit_tag "Search" %>
<% end %>
and
#routes.rb
match '/search', :to => 'posts#search'
Thanks.
See similar questions:
Rails 3 UTF-8 query string showing up in URL?
removing "utf8=✓" from rails 3 form submissions
Basically, to remove 'commit=Search' add :name => nil to the submit_tag. IE needs the utf8 character. However, the second link has a initializer method to remove that part.
In this video, Ryan Bates talks about the name: nil fix (without ajax): http://railscasts.com/episodes/37-simple-search-form
You cant just remove it from url as far as YOU send it.
To clean up will_paginate try this
<%= will_paginate #whatever, params => params.merge({:commit => nil, :utf8 => nil}) %>
I solved utf problem by using
<form action="<%= root_path %>" method="get" >
...
</form>
instead of form_tag, it solved it.
Ryan Bates did a nice screen cast on exactly what you're trying to do (plus some more).
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax
Related
I have a form that the results just show if render instead of redirect.
so the form must not be redirected. someone know if this is possible with rails?
the form is:
Pac: <%= #pac %>
Sedex: <%= #sedex %>
<%= form_tag calculate_ship_path, :method => "get" do %>
<%= text_field_tag :post_code%>
<% end %>
the order controller and action is:
def calculate_ship
frete = Correios::Frete::Calculador.new cep_origem: "#{#order.seller.post_code}",
:peso => "#{#order.product.weight}",
:comprimento => "#{#order.product.weight}",
:largura => "#{#order.product.weight}",
:altura => "#{#order.product.weight}",
cep_destino: params[:post_code]
servicos = frete.calcular :sedex, :pac
#pac = servicos[:pac].valor
#sedex = servicos[:sedex].valor
render '/path/to/rails/app//orders/:id/checkout'
end
and the routes is:
get '/path/to/rails/app//orders/:id/checkout', to: 'orders#checkout', as: :calculate_ship
Rails has a very handy way to handle this: remote forms. The caveat here is that if you're not using Unobtrusive JS (UJS) then this won't work and you'll have to wire it up the hard way.
In form tag notation it would look something like this, presuming you're using Rails 5:
<%= form_tag(calculate_ship_path, remote: true, method: "GET") do %>
<%# form stuff %>
<% end %>
You should be able to do this with most Rails versions, but it might look a little different. What this does, effectively, is submits your form via AJAX. You'll then be able to bind JS event listeners to ajax:success or ajax:error and handle the response you get from calculate_ship.
One thing to note is that when you're doing form submissions, the method is defaults to POST, and probably should be that, a PUT, or a PATCH.
Here's the related docs for rails: http://guides.rubyonrails.org/working_with_javascript_in_rails.html#form-tag
I currently have a page on my rail site that will show, by default, 25 results per page using will_paginate. I would, however, like to have a dropdown menu that allows the user to select the number of results shown from a predetermined list of values. I looked at the answer to a similar question here but the problem I have with the top solution is that I also have a search field in the same page and changing the number of results per page thus removes the search parameter if there is one.
I was wondering if there was any way for me to get around this problem? Thanks in advance!
The select box proved to be troublesome but i found a different way to do the same functionality.
So by using anchor tags and grabbing the search params when building the path it preserves the search criteria as well as any new per_page value.
so 5 and 10 page links would look like this
<%= Link_to "5", current_path(:per_page =>5,:search => params[:search])%>
<%= Link_to "10", current_path(:per_page =>10,:search => params[:search])%>
and the URL in the href will look like this: whatever.com/current?per_page=10&search=DevOps changing with whatever search term is provided.
Be sure to add the per_page value to the controller as well
per_page = params[:per_page]
#items = Item.all.paginate(page: params[:page], per_page: per_page)
Try this:
In your view -
<%= form_tag xyz_path, method: :get do %>
<%= text_field_tag :search, params[:search] %>
<%= select_tag :per_page, options_for_select([10, 20, 50, 100], params[:per_page]) %>
<%= button_tag :submit %>
<% end %>
In your controller -
unless params[:per_page].present?
params[:per_page] = 20 #default
end
if params[:search].present?
ModelName.where(query: params[:search]).paginate(:page => params[:page], :per_page => params[:per_page])
else
ModelName.all.paginate(:page => params[:page], :per_page => params[:per_page])
end
I am getting the error:
Template is missing
Missing template customers/search, application/search with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
This is happening when I'm trying to make a search bar to search through my existing customers in the database. I think it's happening because I am using a partial '_search.html.erb' but it needs to be a partial and I don't know how to fix this problem.
Here is my customers\ _search.html.erb:
<%= form_tag search_customers_path do %>
<input class = "searchbar" id ="custsearch" name=query" placeholder = "find colleague" type="text">
<% end %>
the html it's being rendered with (in events\new.html.erb):
<div class = "searchbar">
<%= render partial: 'customers/search', :object => #customers, locals:{} %>
</div>
here is my customers controller 'search' method:
def search
#q = "%#{params[:query]}%"
#customers = Customer.where("first_name LIKE ? or last_name LIKE ? ",#q,#q)
render :layout => false
end
and here is my routes file:
root 'pages#home'
get '/main' => 'pages#main'
get '/signup' => 'customers#new'
resources :customers do
collection do
get 'search'
end
end
get '/compose' => 'events#new'
resources :events
I'm not even sure if this search will work, but this is the first hurdle to achieving it. Please help!
Thanks
Your search action looking for customers/search.html.erb but you don't have any. Try create a new file in customers folder with name search.html.erb and paste your below code there:
<div class = "searchbar">
<%= render partial: 'customers/search', :object => #customers, locals:{} %>
</div>
also remove render :layout => false from your search action. If you are sending request in html format.
#punitcse's answer was right:
def create
# this will be looking for customers/search.html.erb
# you have to explicitly define it:
render partial: "search", layout: false
end
This should resolve the error.
Calling the partial in the view is pretty normal:
<%= render partial: "customers/search", object: #object %>
The issue looks like it's caused by your create action, which can be corrected to resolve it.
You just need to modify you controller to something like
def search
#q = "%#{params[:query]}%"
#customers = Customer.where("first_name LIKE ? or last_name LIKE ? ",#q,#q)
render partial: "customers/search", locals: { customers: #customers }, :layout => false
end
In cutomers/_search change to
#customers/_search.html.erb
<%= form_tag search_customers_path, method: :get do %>
<input class = "searchbar" id ="custsearch" name=query" placeholder = "find colleague" type="text">
I have a form below displayed on /parties and would like to take whatever a user inputs in this form and add it to the url. I.e. if they searched for "hello" they would be redirected to parties/hello
<h1>Search for a Party</h1><br>
<%= form_tag({:action => "search"}, {:method => "get"}) do %>
<%= label_tag :q, "Enter Playlist Code:" %>
<%= text_field_tag(:q) %>
<%= submit_tag("Find Playlist", :name => "submit") %>
<% end %>
What is the best way to do this?
The logic would go into the PartyController.
Something like this in (syntax may be a little off):
PartyController.rb
def search
result = params[:q]
redirect_to '/parties/' + result
end
yeenow123 gave you a right way, I just want to clarify some points.
"parties/hello" will confuse the routing with parties/:id (show action)
If you don't want to use show action, you should except this from your route.rb: parties resource or you can use show action as function which has params[:id] is your search key and keep working on this.
this question is related to routing-filter.
I have this in my view:
<% form_for :post, :url => {:action => "show"} do |f| %>
which translates in the browser to this:
<form action="/en/posts/show" method="post">
after changing the I18n.locale e.g.
I18n.locale = :en
the html becomes:
<form action="/en/posts/72" method="post">
and the action is not working and I get this error:
Unknown action
No action responded to 72.
Sure, there is no action like 72. This number is the show action's input of course. And it is correct post number. So if I put this address localhost:3000/en/posts/72 to the browser, then it gives me the page without a problem.
So why it doesn't work in the form then?
Thanks.
Specify a method like that:
<% form_for :goal, :url => { :action => 'show' }, :html => {:method => :put} do |f| %>
It worked for me.