It is a memorandum for CSV importing pull-down menu items when submitting a form in Rails and displaying them using DB data.
CSV import related https://qiita.com/Ryuta1346/items/c21cb70b9879c66c8639 https://qiita.com/SoarTec-lab/items/50e046ea2a2764c12c21 https://docs.ruby-lang.org/ja/latest/class/CSV.html https://qiita.com/3yatsu/items/416411c0a8f696dbf99e https://qiita.com/rllllho/items/672e336a03335cba6b34
Pull-down menu related https://qiita.com/HrsUed/items/56677d6c266d8a53ffa7 https://qiita.com/kawakami_shotaro/items/11a677bf34136cb7686d https://qiita.com/colorrabbit/items/b58888506e41d1370fd1 https://crieit.net/posts/Rails-collection-select https://qiita.com/_akira19/items/c218186983f444c2d794
Country code list CSV https://qiita.com/tao_s/items/3yy2b90a2751bfbdd585ea
It is an image of appearance ↓
The pull-down menu choices are displayed using the column information saved in the DB ↓
I will explain CSV import and form pull-down separately.
rails
commandThe import process is performed using the CSV library of Ruby. This time, I imported the country name list from the following CSV. I added the created CSV file to the root directory.
country.csv
number,Country / region name,ISO 3166-English name in 1,number,Three characters,Two letters,place,Each administrative division
1,Iceland,Iceland,352,ISL,IS,Northern Europe,ISO 3166-2:IS
2,Ireland,Ireland,372,IRL,IE,Western Europe,ISO 3166-2:IE
For CSV, I used Country Code List CSV.
I created the following model. The region column is not used this time.
attribute | type |
---|---|
country_name | string |
region | string |
#Country modeling
$ rails g model Country country_name:string region:string
The import process has been added to seeds.rb. There seems to be a separate file and How to execute with runner command.
seeds.rb
require "csv"
#Specify the path to the CSV file as an absolute path
CSV.foreach("country.csv", headers: true) do |row|
Country.create!(
country_name: row["Country / region name"],
region: row["place"]
)
end
Perform a CSV import with the rails
command.
I also wrote other processing in the seed file, so this time I deleted everything in the DB and imported it again.
#This is all you need to import a CSV and save it to your DB
$ rails db:seed
#Erase the DB and reinsert everything
$ rails db:migrate:reset
$ rails db:seed
#Check if it was imported in the rails console
$ rails c
#Check the number of imported country names
> Country.count
249
Modify the View file of the form submission at the time of new posting.
Ruby:view/model_names/new.html.erb
#Before correction
<%= form_with model: @model_name do |f| %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= f.submit "Send" %>
<% end %>
Ruby:view/model_names/new.html.erb
#Revised
<%= form_with model: @model_name do |f| %>
<%= f.label :country %>
<%= f.collection_select :country, Country.all, :id, :country_name, prompt: "Please select a country" %>
<%= f.submit "Send" %>
<% end %>
# collection_select syntax
<%= f.collection_select <Column name of save destination>, <Array data for display>, <Column name of the value to save>, <Column name for display>, <option> %>
Please select a country
with prompt option`Here The explanation about how to use collection_select
is very easy to understand.
Recommended Posts