Starting a [rails] WebApp
First Decisions
Why this post ?
short story
I love learning and my latest learning project includes documenting what I do, making it public might make someone enjoy the adventure with me or even learn a little thing here or there :)
long story
Check it out at the repository I created to host my experiments (soon to be a website)
Initial Stack
I am reasonably sure that the stack will evolve since I will want to experiment and learn other languages as I progress, but in my case, I knew that I would start with a Rails Back End.
The remaining question was whether I would add a fancy js framework or keep it simple at first. I have decided to use Stimulus to handle my javascript needs since it is lightweight and flexible enough for my initial requirements.
React, Vue, Angular, and other frameworks are something that I will want to explore in more detail in the future, but I did not want to start killing flies with a bazooka since the first day.
Starting a new rails project
To start a new rails project, you need to have rails installed (duh!) I won’t bore with the details when they explain how to do it better than I:
https://guides.rubyonrails.org/v5.0/getting_started.html
In my case, I chose to customize my rails new APP_NAME
command with :
-d postgresql
I want PostgreSQL as my database instead of sqlite3 since I know I am going to use Heroku at least at first, and their default database is postgresql
--skip-action-mailer
I am not planning on sending automatic emails, so what is the point?
--webpacker stimulus
As stated above, I wish to use Stimulus at least initially, so it makes sense to configure webpacker for it out of the box.
My final rails command was:
rail new learning-port -d postgresql --skip-action-mailer --webpacker stimulus
Linking to a GitHub repository
Once you have your rails project, you can navigate to it from your terminal:
cd project-name
Create a new repository in GitHub, and then add the remote in your terminal:
$ git remote add origin remote repository URL# Sets the new remote$ git remote -v# Verifies the new remote URL
You can find the remote repository URL at several places, but the most obvious is on the homepage of your project
Clean-er code
On top of the default rails gems, there are some gems that I both want to learn more of and that I believe will get me to write better and cleaner code:
RSpec
I hesitated between the default rails Unit::Test, but I already know some RSpec syntax, and I want to get better at it.
rubocop
So I can have a style guide to follow. I stayed with the default options, but I might tweak them to suit me better in the future.
Guard
Guard allows you to run the tests for a file whenever you save either a file or the RSpec relating to it
regarding the plugins, I use:
guard-rails => runs rails s when guard starts
guard-RSpec => to use guard with RSpec
guard-rubocop => automatically check style on save (I might get rid of this one if it becomes cumbersome)
Personal preference
Slim
I enjoy writing my partials with slim. According to Nate Berscopec in his Complete Guide to Rails Performance, Slim is slower than erb.
But the comfort of use makes it worth it for me.
Changing generators
Finally, the last step before actually starting writing my web app:
clean the generators.
The majority of the generators are ok with me, but I plan to use BEM for my CSS, so it does not make sense to have a CSS file generated every time.
In config/application.rb I added:
config.generators do |g| g.stylesheets falseend
You can find a more in-depth explanation of generators here.
Thanks for reading !
Today is my first foray into blog posts, so please do give me feedback :)
And while my website is not up, you can see the code described in this post on the public repo.
The readme also explains the goal of this posts