Rails - passing arbitrary parameters into search - ruby-on-rails

I have passed a value into a search page via -
<%= link_to 'add', users_path(bookto: #book.id) %>
in the view and
#book = Book.find_by_id(params[:bookto])
in the receiving controller action.
I have a search form in my receiving (index) view
<%= form_tag users_path(params[bookto: #book.id]), method: 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
<%= "#{#book.id}, #{#book.title}" %>
<% if #users %>
<%= render partial: 'layouts/showusers', locals: {users: #users} %>
<% end %>
When I navigate through to the page
http://localhost:3000/users?bookto=1
The value of #book is passed properly. However, when I perform a search, the parameter is not being passed to
http://localhost:3000/users?utf8=✓&search=mysearch
I'm not passing the parameter through. I don't want to use this arbitrary parameter in the search, I just want it available to me to use once the search is complete. How do I achieve this? Do I need to add a search action to my controller?

Why don't just add a hidden field inside your search form like this
<%= form_tag users_path, method: 'get' do %>
<p>
<%= hidden_field_tag :bookto, #book.id %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
Because you would want to see bookid in your URL anyway, so this method is ok in your case.

Related

Ruby on Rails ransack gem search

i want to implement a search that goes through multiple models.
Found this stackoverflow question here that uses ransack and tried it right away. But I can't seem to get it to work.
in my controller :
def search
#quotes = Quote.search(title_cont: q).result
#books = Book.search(title_cont: q).result
#users = User.search(username_cont: q).result
end
routes
get '/search', to: 'application#search'
view
<%= form_tag search_path, method: :get do %>
<%= f.label :title_cont %>
<%= f.search_field :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
You should use params[:q] instead of q. This should work
def search
#quotes = Quote.search(title_cont: params[:q]).result
#books = Book.search(title_cont: params[:q]).result
#users = User.search(username_cont: params[:q]).result
end
Also, f.label and f.search_field doesn't work with form_tag. You should use label_tag and search_field_tag instead
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
Why do you have two fields in your search form, you should only have one search field.
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :q %>
<% end %>

How to use an elasticsearch form from a home page in rails

I have an application set up with articles, a home page, and a search function. I am using elasticsearch for my search functionality. I haven't been able to come up with a way to use the search form on my home page to redirect to the query results. I feel like this should be simple, but I'm just missing it.
Note: Elasticsearch works, just within it's own view folder. I simply want the search form on my home page to operate the same way and take me to a "results" page that shows a list of queried articles.
My search controller:
class SearchController < ApplicationController
def search
if params[:q].nil?
#articles = []
else
#articles = Article.search params[:q]
end
end
end
My search view:
<h1>Articles Search</h1>
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
</p>
<% end %>
<ul>
<% #articles.each do |article| %>
<li>
<h3>
<%= link_to article.title, controller: "articles", action: "show", id: article._id%>
</h3>
</li>
<% end %>
</ul>
The search form on my home page:
<div class="search">
<%= form_for search_path, method: :get do |f| %>
<%= text_field_tag :q, params[:q], placeholder: "Search..."%>
<%= submit_tag "Go", name: nil %>
<% end %>
</div>
My search route:
get 'search', to: 'search#search'

Posting a variable to another controller

I have a welcome controller/view.
On the index.html.erb I have a simple form that takes in one value:
<%= form_tag do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Upon hitting the submit button I'd like to pass the zip variable to another controller called "theaters". The variable doesnt need to be saved in any kind of model, its just being used to execute an API call in the Theaters controller.
Whats the simplest way to do this?
Thanks
Try this:
<%= form_tag({controller: "theaters", action: "index"}) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Hope it helps.
Make a route for your method in TheatresController, lets assume your method name is get_zip
post '/get_zip' => 'theaters#get_zip', as: "zip"
Create your form
<%= form_tag(url: zip_path, method: :post) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Access it inside your method:
def get_zip
#zip = params[:zip]
#your logic
end

link submit to another page

I have this form:
in header of my website:
<% form_tag request.path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
controller:
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
#users1 = User.simple_search(params[:query]).all
end
model:
acts_as_simply_searchable :columns => [:name, :email]
view
<%= will_paginate %>
<ul class="users">
<%= render #users1 %>
</ul>
<%= will_paginate %>
displays a user
I want to link the submit button to index.html.erb (i have assigned a path in routes.rb). So that the user can look at the search results
You don't do this at the submit button, but at the form_tag URL (the first parameter). It would be something like this:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
Simply change request.path to the desired path.
I think you are missing the '=' sign and string interpolating the 'request.path'
This Worked for me:
<%= form_tag "#{request.path}", method: "get" %>
...the rest of your code ...
<% end %>

How to params[] value to a user-defined action

I have a form named purchase_products.html.erb
<%= form_for (:purchase) do |f| %>
<% for product in #purchase %>
<%= check_box_tag "product_ids[]", product.id %>
<%= product.product_name %> |
<%= product.description %> |
<%= product.link %> |
<%= link_to 'Show', product_path(product) %><br/>
<% end %>
<%= f.submit "Purchase" %>
<%= link_to 'Home', :start %>
<% end -%>
Here when I click the submit button I want to send all selected checkboxes values to a action 'my_purchase' in purchase controller.
How do i redirect my button to call my user defined method?
and how can I do to retrieve which checkboxes are selected?
Add url params to form_for. See form_for API
<%= form_for(:purchase, :url => {:action => :user_defined_method}) do |f| %>
You can access the checkboxes as an array of product ids in the method as params[:product_ids].

Resources