, Rails new command will write a series from application creation to deployment.
Last time https://qiita.com/gonshiba-n/items/abd1b11e35f99eb5975f
config/application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module xxxxxx
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
#from here
config.time_zone = 'Asia/Tokyo' #Japan time setting
config.i18n.default_locale = :ja #Japanese setting
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s] #Ja at startup.Have yml read
#Add up to here
#-abridgement-
end
end
Now that you have set the Japanese localization, let's get the file to read! Is the terminal now in the Rails working directory? If so, enter the following command to get the Japanese localization file from GitHub.
Terminal.
curl -s https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -o config/locales/ja.yml
Next is Japan time. Create a new time_formats.rb in the initializers directory inside the config directory.
Terminal.
touch config/initializers/time_formats.rb
Now you can create it! Open time_formats.rb and write the code below and you're ready to go.
config/initializers/time_formats.rb
Time::DATE_FORMATS[:datetime_jp] = '%Y year%m month%d day%H o'clock%M minutes'
Actually, by using it as follows It will be displayed like 12:00 on September 25, 2020.
<%= xxxxx.created_at.to_s(:datetime_jp) %>
<%= xxxxx.updated_at.to_s(:datetime_jp) %>
I've summarized the introduction of Rspec in the past, so please check it out! https://qiita.com/gonshiba-n/items/36c91e000fdc9b18aa28 However, if you write Rspec in the development environment and the test environment, you can use the generator that creates the test file, so it seems better to put it in the group below!
I will also add Rubocop. Rubocop is called a linter, and it's a great way to check if your code is out of the rules or if you have a typo!
Gemfile.
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
#Here are the test gems including Rspec
gem 'rspec-rails'
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'factory_bot_rails'
gem 'rubocop-airbnb'#← Linter
end
If you write another gem you want to use
Terminal.
bundle
Do the above!
For more information on Rubocop, please refer to this article! https://qiita.com/shunsuke_sasauchi/items/70ccc4b02be72640f699
Set the page that is displayed first when you access the rails app. Right now, it's just a template and nothing is inside, so I'll create it. Let's make various things such as View and controller with rails g controller.
Terminal.
rails g controller StaticPages top about login
Sorry for my example! With this, rails created this and that automatically.
Next, set the ruding. Open config / routes.rb!
config/routes.rb
Rails.application.routes.draw do
resources :static_pages#← This is leaving the resource to erase the code created by rails
root 'static_pages#top'#← Add this
end
This completes root setting! Start the server as a trial http://localhost:3000 If you access and look at, you will see the page you set.
In the production environment, the dynamic display of the image is off, so change it to on!
config/environments/production.rb
config.assets.compile = true #False by default
end
This is OK!
Please register on heroku! I think I needed a credit card, but if you search for it, I think there is an insanely easy-to-understand article, so please refer to that! Since I am using PostgreSQL, I have not made any settings around the DB. Please note that if you use another DB, you will need to set it separately.
First, let's log in after making heroku available with the CLI!
Terminal.
heroku login
I will create an application on heroku. Since the app name is incorporated into the URL as it is, characters that cannot be used in the URL cannot be used! And you can't use the app name that has already been used!
Terminal.
heroku create favorite app name(To be used in URLs_I can't use)
Finally, push and deploy to heroku's remote repository.
Terminal.
git push heroku master
If you have a DB migration file, please migrate it with the following command.
Terminal.
heroku run rails db:migrate
At this point, let's actually access the URL and check it!
Thank you for reading this far. Last time, I deployed it just before the application was completed, so I had a lot of trouble. It's definitely easier to deploy after setting up the application before development, I think that the sense of security that you can develop while checking the operation in both the development environment and the production environment is tremendous. So far, I've written only the procedure, but I can't! I got an error! Or something wrong! In that case, I would appreciate it if you could teach me.
Recommended Posts