link submit to another page - ruby-on-rails

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 %>

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 %>

Rails 4 Filter Index lists with toggle

I have created a small ticketing system for my users. I have tickets as New, In Progress and Completed. I would like to be able to toggle on the page whether the completed are hidden or shown. What is the best way to do this? The param that holds these values is ticket.status.
In Controller:
def index
#tickets = Ticket.all
if params[:filter_by]
#tickets = Ticket.where(:category => params[:filter_by])
else
#tickets = Ticket.all
end
end
In Index view:
<div>
<div><%= link_to "Full Listing", tickets_path %></div>
<div><%= link_to "Admin", tickets_path(:filter_by => :Administrative), {:method => :get} %></div>
<div><%= link_to "Graphics", tickets_path(:filter_by => :Graphics), {:method => :get} %></div>
<div><%= link_to "IT", tickets_path(:filter_by => :IT), {:method => :get} %></div> |||
<div><%= link_to "New entry", new_ticket_path %></div> |||
<div>
<% if current_user.present? %>
<%= link_to " Sign Out", sessions_path, method: :delete %>
<% end %>
</div>
How about you take a look at Ransack, SimpleForm and SimpleFormRansack? That is how I build most of my own index filters.
https://github.com/activerecord-hackery/ransack
https://github.com/plataformatec/simple_form
https://github.com/kaspernj/simple_form_ransack
In your controller do something like this:
def index
ransack_params = params[:q] || {}
ransack_params[:status_eq_any] ||= ["New"]
#ransack = Ticket.ransack(params[:q])
#tickets = #ransack.result
end
In your view you could do something like this:
<%= simple_search_form_for #ransack do |f| %>
<%= f.input :category_eq_any, collection: ["Administrative", "Graphics", "IT"] %>
<%= f.input :status_eq_any, collection: ["New", "Completed"] %>
<%= f.button :submit %>
<% end %>
<% #tickets.each do |ticket| %>
...
<% end %>
You can also show links to predefined searches like this:
<%= link_to "Admin", tickets_path(q: {status_eq: "Administrative"}) %>

Ruby on rails: Multiple submit_tag on the same form

How can I have multiple submit_tag buttons on the same form?
For now I got it working only for one button, but I'm not sure how to get form_tag to handle multiple paths.
Routes.rb
resources :actions do
end
root 'home#start'
match '/home/add', to: 'home#add', via: 'get'
match '/home/subtract', to: 'home#subtract', via: 'get'
match '/home/multiply', to: 'home#multiply', via: 'get'
Start.html.erb
<%= form_tag "/home/add",:method => "get" do %>
<p></p>
<p>
<%= label_tag :entered, "Please enter value:" %> </br>
<%= text_field_tag :entered %>
</p>
<p></p>
<p>
<%= label_tag :entered2, "Please enter value:" %> </br>
<%= text_field_tag :entered2 %>
</p>
<%= submit_tag "add", :controller => "home", :action => "add" %>
<%= submit_tag "subtract", :controller => "home", :action => "subtract" %>
<%= submit_tag "multiply", :controller => "home", :action => "multiply"%>
<% end %>
Please advise.
Thank you in advance.
I don't know if you can make it go to different path. But would something like this help?
Just have one action and do stuff in your controller based on the submit button that was clicked. You routes will look like
*Routes.rb*
resources :actions do
end
root 'home#start'
match '/home/operation', to: 'home#add', via: 'get'
You view will be
<%= form_tag "/home/operation",:method => "get" do %>
<p></p>
<p>
<%= label_tag :entered, "Please enter value:" %> </br>
<%= text_field_tag :entered %>
</p>
<p></p>
<p>
<%= label_tag :entered2, "Please enter value:" %> </br>
<%= text_field_tag :entered2 %>
</p>
<%= submit_tag "Add"%>
<%= submit_tag "Subtract"%>
<%= submit_tag "Multiply"%>
<% end %>
In your controller
class HomeController < ApplicationController
def operation
send(params[:commit].downcase) #params[:commit] will have one of the values "Add", "Subtract", "Multiply"
end
private
def add
#do something
end
def subtract
#do something
end
def multiple
#do something
end
end

Ruby on rails: If else in controller issue

So I am having an issue with an if else statement in the controller. I have 4 radiobuttons on my page and one hidden field. There are also 4 divs where only one can be visible at a time.When a different radiobutton is checked another div is shown. This should work like a 'complex' search interface. In the hidden field I'm inserting a value from 1 to 4 (depending on what radio button is checked). In the controller I'm looking at the value of the hidden field and my functions should change accordingly. My problem is that it does not work. I tried a couple of different things but didn't find an answer to my problem.
Here's my code
View:
<div>
<%= radio_button_tag 'searchRBN', 'patient', true, :onchange => "checkRadioButton()" %>
<%= label_tag :byPatient_patient, "Patient" %>
<%= radio_button_tag 'searchRBN', 'staff', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byStaff_staff, "Staff" %>
<%= radio_button_tag 'searchRBN', 'ocmw', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byOcmw_ocmw, "OCMW" %>
<%= radio_button_tag 'searchRBN', 'mutuality', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byMutuality_mutuality, "Mutuality" %>
</div>
<%= hidden_field_tag(:hidden_one, "1") %>
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
<div id="searchByStaff" class="notVisible">
<%= form_tag patients_path, :method => 'get' do %>
<%= text_field_tag :search2, params[:search2] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</div>
Controller:
def index
#staff_all = Staff.all
#ocmw_all = Ocmw.all
#mutuality_all = Mutuality.all
if params[:hidden_one] == '1'
#patients = Patient.searchByName(params[:search1])
elsif params[:hidden_one] == '2'
#patients = Patient.searchByStaff(params[:search2])
else
#patients = Patient.all
end
end
In my model:
def self.searchByName(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
def self.searchByStaff(search)
if search
find(:all, :conditions => ['marriedTo LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Mathias A.
I think your hidden field is outside of your form tag so it will be never submitted to the server.
Another solution would be to simply put a hidden field to each search form you have to identify the corresponding search form on the server.
Example:
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<%= hidden_field_tag :search_type, :search_by_patient %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>

remember form tag input values

I'm trying to create a filter form that user selects dates and sources. The problem is after clicking submit button, in the new page i see that input values are empty. Is there a way to make form remember its values ? Thanks.
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from %>
<%= text_field_tag :to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>
controller
def index
#from = params[:from] ? params[:from].to_datetime : (Time.now-3.day)
#to = params[:to] ? params[:to].to_datetime : (Time.now)
#sources = params[:sources] ? params[:sources] : 1..6
#products = Product.where(:source_id => #sources, :created_at => #from.beginning_of_day..#to.end_of_day)
end
Can't you use the value and checked options from these tags? Here is an example :
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from, #from %>
<%= text_field_tag :to, #to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id, #sources.include?( source.id ) %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>

Resources