This content originally appeared on DEV Community and was authored by Yaroslav Shmarov
MISSION: field to search for user email that contains characters. Example:
users_controller.rb
def index
if params[:email]
@users = User.where('email ILIKE ?', "%#{params[:email]}%").order(created_at: :desc) #case-insensitive
else
@users = User.all.order(created_at: :desc)
end
end
any view (users/index.html.erb or in a bootstrap navbar)
.form-inline.my-2.my-lg-0
= form_tag(courses_path, method: :get) do
.input-group
= text_field_tag :title, params[:title], autocomplete: 'off', placeholder: "Find a course", class: 'form-control-sm'
%span.input-group-append
%button.btn.btn-primary.btn-sm{:type => "submit"}
%span.fa.fa-search{"aria-hidden" => "true"}
without bootstrap
<%= form_tag(users_path, method: :get) do %>
<%= text_field_tag :email, params[:email], autocomplete: 'off', placeholder: "user email" %>
<%= submit_tag "Search" %>
<% end %>
That's it! Looks nice, doesn't it?
This content originally appeared on DEV Community and was authored by Yaroslav Shmarov
Yaroslav Shmarov | Sciencx (2021-04-23T12:08:43+00:00) TLDR – Basic search field with Ruby on Rails. Retrieved from https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.
