Use collection_select to pull down the data stored in Active_Hash

Overview

What I want to implement this time is a function that stores unchanged information such as the shipping source and product status as Active_Hash and displays it as a pull-down in the view file when implementing the product listing function.

Regarding how to store prefectures etc. with Active_Hash, I wrote a separate article, so please refer to that.

About Active Hash

What is collection_select

It is one of the helper methods, and it is a description that can display the value given the key in the select box.

The basic description is

ruby:xxx.html.erb


f.collection_select(:Column name,Array of elements,Item of value attribute,Text item, { prompt: "Please select" }

It seems to be.

Example

This time, I will write down an example of implementing the presence or absence of shipping charges as a PostageType class when listing products.

ruby:products/new.html.erb


<%= f.collection_select(:postage_type, PostageType.all, :id, :name, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>

If you leave the prompt part empty, Active Hash's: id = 1 seems to be displayed as the pull-down default.

I will also write down the description of Active Hash of postage_type.

models/postage_type.rb


class PostageType < ActiveHash::Base
  self.data = [
    {id: 1, name: '---'},{id: 2, name: 'Cash on delivery(Buyer burden)'},{id: 3, name: 'postage included(Exhibitor burden)'}
  ]
end

As an aside, let's write the ** class name in the upper camel case **. I made an error and got lost once ...

Recommended Posts

Use collection_select to pull down the data stored in Active_Hash
[Swift] Use UserDefaults to save data in the app
How to use collection_select
How to use active_hash! !!
How to use the getter / setter method (in object orientation)
How to use JSON data in WebSocket communication (Java, JavaScript)
How to create a placeholder part to use in the IN clause
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
[Rails] How to display information stored in the database in view
I tried to sort the data in descending order, ascending order / Rails
[Rails] Don't use the select method just to narrow down the columns!
How to use Lombok in Spring
How to use the form_with method
Pass the i18n locale to JavaScript
How to use Spring Data JDBC
How to use the wrapper class
Use the Findbugs plugin in Eclipse
How to use InjectorHolder in OpenAM
From introduction to use of ActiveHash
How to use classes in Java?
Show all data stored in UserDefaults
How to change the maximum and maximum number of POST data in Spark
[Rails] Use devise to get information about the currently logged in user
I tried to make full use of the CPU core in Ruby
To you who were told "Don't use Stream API" in the field
Is it possible to put the library (aar) in the Android library (aar) and use it?
Shorten the UUID to base64 in Swift.
Multilingual Locale in Java How to use Locale
How to get date data in Ruby
How to use custom helpers in rails
Java reference to understand in the figure
[Java] How to use the hasNext function
How to use named volume in docker-compose.yml
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
[Java] Use cryptography in the standard library
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
[Processing × Java] How to use the class
To write Response data directly in Spring
How to get the date in java
[Processing × Java] How to use the function
How to use environment variables in RubyOnRails
I want to use @Autowired in Servlet
[Java] How to use the Calendar class
Understand in 5 minutes !! How to use Docker
How to overwrite Firebase data in Swift
How to use credentials.yml.enc introduced in Rails 5.2
Steps to Use JConsole in Cognos Analytics
How to use ExpandableListView in Android Studio
Use hashes well in Ruby to calculate the total amount of an order
Use the where method to narrow down by referring to the value of another model.
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
You may not want to use the remove method in ArrayList very often
[Java] Various methods to acquire the value stored in List by iterative processing
How to make a unique combination of data in the rails intermediate table
Precautions when saving data in Realm (distinguishing between Managed / Unmanaged, when to use copyToRealm ())