I've followed this tuto to make a search form in my app.
It works well but it doesn't do exactly what I want
At the moment, I've to go to /search to see my result but I want that when a search is made, everywhere in my app, it's redirect on /search and the results are displayed.
My search controller :
def search
if params[:q].nil?
#idees = []
else
#idees = Idee.search params[:q]
end
end
My search form view
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
And my route :
get '/search', to: 'search#search'
It has to be a problem with my route but I don't know how to do this
Update your form as:
<%= form_for :search, url: search_path, method: :get do |f| %>
You may need to update you search action too to support nested params.
Related
I feel like there's no easy way to do this in rails, but since I'm fairly noob in rails I decided to ask for solutions:
I have a form in a view that contains a single (text) input. How can I specify the form url such that it will do a GET to /something/<input> ?
I know I could:
use custom javascript code
post to an endpoint that would do the redirect
Is there any cleaner way?
(using rails 5.2.1 if relevant)
I think you could use something like this:
<%= form_tag(route_path method: :get) do %>
search <%= text_field_tag :search %>
<%= submit_tag 'Search' %>
<% end %>
It depends on which form helpers you use.
Maybe you can try this
In your form:
<%= form_with(url: "path_name/input_field_name", method: :get, local: true) do |f| %>
<%= f.text_field :input_field_name %>
<%= f.submit 'Search', input_field_name: nil %>
<% end %>
In your controller:
unless params[:input_field_name].blank?
#results = ModelName.where('input_field_name iLIKE ?', "%#{params[:input_field_name]}%")
end
I'm trying to implement a two search form_tag on a the same page, each search form is placed inside dynamic bootstrap tabs. The first one which is working is basic a search form with one field. The second one which is not working has two fields, one is the same search method as the first and the other I'm trying to get the address from the other_location field and via params[:other_location].
With the current setup the other_location field form the second form does not appear!
Both of the forms are inside partials and I am rendering them inside two dynamic bootstrap tabs like this:
<%= render 'pages/search' %>
<%= render 'pages/search_other' %>
<%= form_tag search_items_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], autofocus: true,
class: "search-query search_size",
placeholder: "Enter product to search" %>
<%= submit_tag "Search", name: nil, :style => "display: none;" %>
<%end%>
<%= form_for :search_other_path, :method => "get" do |form| %>
<%= form.text_field :search, autofocus: true,
class: "search-query search_size",
placeholder: "Enter keyword to search" %>
<% form.fields_for :other_location_path, :method => "get" do |f| %>
<%= f.text_field :other_location, class: "search-query search_size",
placeholder: "Enter address to search" %>
<%= form.submit "Search", name: nil, :style => "display: none;" %>
<%end%>
<%end%>
model
def self.search(search)
return where("0=1") if search !~ /\w{4}/
where("lower(title) LIKE lower(:term)", term: "%#{search}%")
end
routes.rb
get 'search' => 'pages#search', as: 'search_posts'
get 'search' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#other_location', as: 'other_location'
controller:
def search_other
if params[:search]
#posts = Post.near(other_location,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
def other_location
other_location = params[:other_location]
if params[:other_location]
Geocoder.search(params[:other_location])
end
end
def search
if params[:search]
#posts = Post.near(action,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
On your route file:
get 'search/other' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#search_other', as: 'search_other_items'
both GET requests are going to your pages_controller.rb #search_other method. So even if you have the two form_tags sending the data to different paths (search_other_path, and search_other_items_path) it would be going to the same controler method - which is redundant.
On your actual HTML you have two form tags:
<%= form_tag search_items_path, :method => "get" do %>
and
<%= form_tag search_other_items_path, :method => "get" do %>
You have not mentioned search_items_path in your routes, so I have no idea where that's pointing to. Likely its a proper controller that works since you mentioned the first form was the only one working.
Now, your mentioned controller only has a search method. So to start you are looking at the wrong controller. You should be looking at the controller methods being referenced by the form's action.
In this case, the second form is sending it's request to search_other_items_path which according to your routes, its pointing to pages_controller.rb -> #search_other method.
You should edit your question to include code that is actually relevant. Maybe then I can actually help.
I have a search form on my app:
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", name: nil %>
</p>
<% end %>
that searches by distance:
def search
if params[:search].present?
#las = La.near(action,10).reorder('distance')
else
#las = []
end
end
The results are sorted by distance and all works well up to here!! The only issue here is that the results do not appear according to the keyword typed as well. Thus whatever keyword I type, all results appear and sorted by distance.
Any idea what I might be doing wrong here??
You could try passing the content of params[:search] within the near scope:
#items = Item.near(params[:search], 10).reorder('distance')
I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});
Im new to rails, and I am not able to understand how I can get data from a submitted form.
This is my form registerduser.html.erb
<%= form_tag("/submitform", :method => "get") do %>
<%= label_tag(:q1, "id:") %>
<%= text_field_tag(:q1) %>
<%= submit_tag("Submit") %>
<% end %>
How do I get the submitted values in this customers controller action?
def submitform
#customers_values = params[:q1]
end
routes.rb
get "customers/submitform"
error:
No route matches [GET] "/submitform"
You set wrong form url. It should be:
<%= form_tag('/customers/submitform', method: :get) do %>
...
or:
<%= form_tag(controller: :customers, action: :submitform, method: :get) do %>
...