Initialise the Rails Application
Default Rails setup
-
Ensure that your asdf plugins are up to date with
asdf plugin update --all
. -
Install the latest Ruby version with
asdf install ruby latest
(Check if it's supported by Heroku). -
Switch your global Ruby to the fresh one:
asdf global ruby latest
. -
Run
gem update --system
to update Ruby's default gems (e.g.bundler
). -
Check if you are using the latest stable version of Rails with
rails -v
and update it if you are not. You can do this withgem update rails
. Beware of beta versions. -
Start a new Rails project using
rails new [project-name] --database=postgresql --skip-ci --no-skip-test --skip-action-mailbox --template https://raw.githubusercontent.com/renuo/applications-setup-guide/main/ruby_on_rails/template.rb
where the project-name
is exactly the one you chose before.
⚠️ You may want to choose a different database than Postgres, but most of the time this will be your choice.
If you do not need a DB you may rethink the fact that you may not need Rails at all: Take a look at Sinatra or Angular
You might also need actionmailbox of course, so always double-check the parameters that you are using.
⭐️ This setup does not include either js-bundling nor css-bundling by default.
It will start with the simplest possible Rails setup and will use sprockets and importmaps.
If you need to do fancy stuff, discuss with your team the opportunity of including a js-bundling and css-bundling tool.
We want to go "no build" whenever possible.
-
Run
bin/setup
-
Run
bundle exec rails db:migrate
to generate an emptyschema.rb
file. -
Then check your default Rails setup by running
rails s
and visiting http://localhost:3000. You should be on Rails now, yay! -
Finally check if http://localhost:3000/up is green.
Adjustments
Some adjustments are made automatically by the template, but you should check them. Some other adjustments must be performed manually.
Automatic adjustments
⭐The Gemfile reads the required ruby version from the
.ruby-version
file. This is used by Heroku to determine what version to use. Deploio reads the ruby version from the Gemfile, with the .ruby-version file inlined into it. https://paketo.io/docs/howto/ruby/#override-the-detected-ruby-version
⭐️renuocop replaces the default rubocop-rails-omakase. We have our own set of rules at Renuo. You can discuss them at https://github.com/renuo/renuocop and you can also contribute to them.
⭐️a bin/check script is added to the project. This script will run all the tests of the project. It is used in our CI and can be used locally to check if everything is fine. You can customize it to your needs.
⭐️a bin/fastcheck script is added to the project. This script will run all the linters of the project. It is used in our CI and can be customized to your needs. It will be used as a hook before pushing to quickly check for linting issues.
⭐️a bin/run script is added to the project. This script will start the application.
⭐️bin/check, bin/fastcheck and bin/run are standardized tools for more convenience at Renuo.
Manual adjustments
Please perform these adjustments manually:
ENV variables with Figaro
-
Add
figaro
to Gemfile. Check the gem homepage to see how to install the gem (usuallybundle exec figaro install
is enough). Delete the newly created fileconfig/application.yml
... -
and create
config/application.example.yml
where you will specify the only environment variable you need for now:SECRET_KEY_BASE
. -
Going forward we will only push the
config/application.example.yml
file to the repository in order to protect our env variables. -
Add application.yml to .gitignore
-
Add the following section to your
bin/setup
script so that theapplication.yml
is created from theapplication.example.yml
when the project is setup locally:puts "\n== Copying sample files ==" unless File.exist?('config/application.yml') system! 'cp config/application.example.yml config/application.yml' end
-
add one more key to application.example.yml
APP_PORT: 3000
Make sure it comes before any
rails
comands. -
To ensure you have all the required keys from the
application.example.yml
in yourapplication.yml
, create the initializer for figaro inconfig/initializers/figaro.rb
:Figaro.require_keys(YAML.load_file('config/application.example.yml').keys - %w[test production development])
-
Run
bin/setup
again.
Configuration customisation
-
Update
config/application.rb
and set the default language and timezoneconfig.time_zone = 'Zurich' # may vary config.i18n.default_locale = :de # may vary
-
Update your
config/environments/production.rb
settings:config.force_ssl = true # uncomment config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "warn") # change
-
Update
config/environments/development.rb
settings:config.action_controller.action_on_unpermitted_parameters = :raise config.i18n.raise_on_missing_translations = true # uncomment
-
Update
config/environments/test.rb
settings:config.action_controller.action_on_unpermitted_parameters = :raise config.i18n.raise_on_missing_translations = true # uncomment config.i18n.exception_handler = Proc.new { |exception| raise exception.to_exception } # add config.active_record.verbose_query_logs = true # add # add the following lines to the end of the file config.to_prepare do ActiveSupport.on_load(:active_record) do ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true end end
-
The default Content Security Policies should not always be activated, but rather only where there are platform secrets that need to be secured. This rule can be overwritten by a customer, if he opted into CSP when selecting his maintenance plans.
-
If you're using a js-bundling tool, let's clean up after asset precompilation to reduce Heroku slug size. Add this to the
Rakefile
:Rake::Task['assets:clean'].enhance do FileUtils.remove_dir('node_modules', true) FileUtils.remove_dir('vendor/javascript', true) end
Finalising
- Check if the following scripts run successfully:
bin/setup
,bin/check
,bin/run
- If they do, commit all your changes to the main branch with Git.