It is very troublesome for beginner engineers to manually enter the dummy data of the portfolio, and I want to enter the dummy data in a cool way. It's easy to do with gem Faker.
The working time will be about 30 minutes for the first time.
Let's do it!
First, let's introduce Faker to Gemfile!
Gemfile
gem 'faker'
Next, install the Japanese localization file of faker. Download or copy ja.yml from the link below, place it in config> locales> ja.yml in rails. https://github.com/faker-ruby/faker/blob/master/lib/locales/ja.yml Next, write `` `config.i18n.default_locale =: ja``` in the module of application.rb.
application.rb
module hoge
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
config.i18n.default_locale = :ja #Add here
end
end
3.bundle install Let's bundle install in the terminal!
Finally, we will put the test data in the seed file. In the following case, 50 random dummy data will be input to the name / character / email column created by model. Check READE.me for the method description of Faker ::!
seeds.rb
50.times do
User.create(
name: Faker::Name.name,
character: Faker::Games::Pokemon.name,
email: Faker::Internet.email,
)
end
#Leftmost name:Is the model column name
Next, use rails db: seed to reflect the data.
Let's check if it is reflected in `` `rails c``` from the console.
User.all
If the data in Japanese is included, it is complete. Thank you for your hard work.
Recommended Posts