TLDR – Basic search field with Ruby on Rails

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-insensitiv…


This content originally appeared on DEV Community and was authored by Yaroslav Shmarov

MISSION: field to search for user email that contains characters. Example:

search-field.png

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » TLDR – Basic search field with Ruby on Rails." Yaroslav Shmarov | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/
HARVARD
Yaroslav Shmarov | Sciencx Friday April 23, 2021 » TLDR – Basic search field with Ruby on Rails., viewed ,<https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/>
VANCOUVER
Yaroslav Shmarov | Sciencx - » TLDR – Basic search field with Ruby on Rails. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/
CHICAGO
" » TLDR – Basic search field with Ruby on Rails." Yaroslav Shmarov | Sciencx - Accessed . https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/
IEEE
" » TLDR – Basic search field with Ruby on Rails." Yaroslav Shmarov | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/tldr-basic-search-field-with-ruby-on-rails/. [Accessed: ]
rf:citation
» TLDR – Basic search field with Ruby on Rails | Yaroslav Shmarov | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.