[RUBY] [Rails] How to use select boxes in Ransack


In Ransack, I will summarize how to use the select box.

About select

First, I will explain the select helper for creating select boxes.

Basic type

select(object,Property name,Element information,option,Element attribute)

Implementation example

<%= f.select :name, [['sample1', 1], ['sample2', 2], ['sample3', 3]],
 {include_blank: 'No selection'}, class: 'sample' %>

(Assuming that it is in form_for, it does not contain an object.) In this way, if you put an array in the third argument, value 1 or 2 will be sent as a parameter.

Useful helper

There is also a useful helper to further customize the select box.

options_for_select If you want to set the select box initial value, use  Ľoptions_for_select`.

Uninflected word


Implementation example

<%= f.select :name, options_for_select({sample1: 1, sample2: 2, sample3: 3}, 1),
 include_blank: true %>

Since "1" is included in the second argument of options_for_select, `1 is displayed as the default value. ``


When you want to create choices from a model, it is convenient to use  Ľoptions_from_collection_for_select`.

Uninflected word

options_from_collection_for_select(Array of objects,value attribute,text item,option])

Implementation example

<%= f.select :name, options_from_collection_for_select(User.all, :id, :name , 1) %>

Now you can automatically make the name in the User model a select box.

How to use with Ransack

Use  Ľoptions_from_collection_for_select` as an example.

Implementation example

<%= search_form_for(@q, url: users_path, local: true) do |f| %>

  <%= f.select :name_eq, options_from_collection_for_select(User.all, :id, :name , 1) %>

  <%= f.button 'Search for' %>

<% end %>

By doing the above, you can search by using the name in the User model as a select box. In addition, the initial value is set as an option.

The _eq part of name_eq is called the matcher and you can search for equal values.

About Matcher

There are also the following matchers.

matcher meaning
_eq equal
noteq Not equal
_cont Including value(LIKE)
_iteq Less than
_gteq that's all

For more information, see the "Search Matchers" section of here.


https://railsguides.jp/form_helpers.html https://shinmedia20.com/rails-select-box

Recommended Posts

[Rails] How to use select boxes in Ransack
How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to use credentials.yml.enc introduced in Rails 5.2
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use devise (Note)
[Rails] How to write in Japanese
How to use Ruby on Rails
How to use InjectorHolder in OpenAM
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
How to use classes in Java?
[Introduction to Rails] How to use render
How to install Swiper in Rails
How to implement search functionality in Rails
Multilingual Locale in Java How to use Locale
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
How to use named volume in docker-compose.yml
[Rails] How to use rails console with docker
How to insert a video in Rails
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use Docker in VSCode DevContainer
[rails] How to configure routing in resources
[Ruby on Rails] How to use redirect_to
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use environment variables in RubyOnRails
[Rails] How to use video_tag to display videos
Understand in 5 minutes !! How to use Docker
[Rails] How to use helper method, confimartion
How to use ExpandableListView in Android Studio
Summary of how to select elements in Selenium
How to use Map
How to write Rails
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use rails g scaffold, functions, precautions
How to use "sign_in" in integration test (RSpec)
How to use collection_select
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]