Now Reading
Welcome to Rails Cheat Sheet · Fly

Welcome to Rails Cheat Sheet · Fly

2023-05-13 07:41:36

A train with a gem painted on the side traveling down the train tracks.
Picture by Annie Ruygt

Rails has been round since early 2004. Again then, the alternatives of constructing net purposes was both working with a bunch of spaghetti code that was SFTP’ed as much as a server or work with an enterprise monstrosity like Enterprise Java Beans. Rails confirmed us that net improvement may very well be enjoyable and sane with built-in options like environments, database migrations, and a Mannequin-View-Controller method to constructing net purposes.

Let’s do some math. The 12 months on the time of this writing is 2023. Rails was created on August of 2004. Rails is over 18 years outdated! And to this present day, in any case this time, you may discover passionate long-time Rails builders who swear by the framework as being the best for constructing and transport purposes, however many have taken with no consideration what it is wish to get began in Rails at this time and the plethora of different frameworks which have caught up.

Why Should You Consider Rails?

Rails (and Ruby) cares a lot about developer happiness and productivity. It optimizes for the great feeling of, “wow, I got a lot done today”. You start by building a web application, which can be integrated into an Android & iOS application (more on that later).

Reading the Rails Doctrine is a superb place to get your bearings to place into context what the Rails neighborhood aspires to be. Regardless of these aspirations, you may generally end up in very disorienting locations in Rails, just like the asset pipeline. Total although, Rails is kind of an pleasurable developer expertise.

Getting Started

If you’re getting started for the very first time with Rails you’ll want to have these resources open and ready in your browser as you build a Rails application.

Rails Guides

Rails Guides are a fantastic place to begin studying in regards to the fundamentals of constructing a Rails software. Even if you’ve moved past the fundamentals, you may nonetheless end up touchdown on guides to grasp how a selected Rails library works.

Rails API

When your journey strikes previous tutorials, you may end up referring to the API docs to see how particular courses or strategies work in Rails. Its a reference that you’re going to wish to preserve useful.

Ruby Docs

Similar to the Rails API, there’s a great deal of stuff that you’re going to be utilizing from the Ruby language and its customary runtime. Typically it is onerous to know what’s in Ruby vs what’s in Rails, particularly due to an enormous library in Rails that extends the Ruby language referred to as Lively Assist.

Chat GPT

As massive language fashions, like OpenAI’s Chat GPT-4 and Google’s Bard, grow to be ever extra succesful, in addition they changing into higher instruments to assist reply questions from people who find themselves beginning out in any new programming language. GPT-4 is a superb useful resource for getting assist, however be warned that its completely pleased giving solutions that appear believable, however are both fully unsuitable, convoluted, or not a greatest follow.

Stuff You’ll Inevitably Bump Into With Rails

It might not say it in the beginners guides, but most people who ship Rails applications are using these technologies in their stack.

Postgres

rails new defaults to sqlite, which is a very nice database, however when it is time to deploy to manufacturing, most individuals select Postgres as their database.

Redis

I joke, “it actually must be referred to as Rails on Redis”, which is type of true. Redis can be utilized in Rails for caches, background job processing, and WebSockets, ActionCable, and Hotwired.

Redis can be a Swiss Military knife. It is nice for caches as a result of it is actually quick to learn and write knowledge to Redis. It additionally ships with pub/sub options that make it appropriate to be used by background employees and pushing notifications to browsers through WebSockets.

Sidekiq

Net requests must be quick, however generally your software must do one thing that takes a couple of minutes, like change the decision of a big video file. The very last thing you need in an internet software is have an internet server take a couple of minutes to satisfy a request—doing so would most likely deliver your net software to a crawl as requests stack up in a queue ready for the server to reply.

The answer in Rails is to dump this request to a background employee, which frees up the net server to proceed taking requests from different shoppers.

Sidekiq is the preferred background employee framework. You possibly can’t go unsuitable utilizing it since its battle examined in manufacturing by many massive Rails purposes.

Hotwire

Hotwire is a part of Rails, however for those who landed on their website you’d solely see Rails talked about as a footnote. Inside Rails, you may see these known as Turbo and Stimulus.

Hotwire was created by the identical individuals who created Rails in an effort to drastically scale back the quantity of JavaScript wanted to be written and deployed to the consumer. This method to constructing low latency interactive net purposes has surged in recognition over the previous few years to fight the growing complexity and frustrations Rails builders skilled when constructing JS net purposes.

The framework is included by default if you create a brand new Rails software.

Ruby Gems & Bundler

On the root of your Rails software you may see a Gemfile. The entries in that file level to packages maintained by a neighborhood of Ruby builders which are hosted on https://rubygems.org.

Screencasts

When you’re tired of reading documentation and simply want to sit on the couch and watch Rails, there’s a few notable screencasts worth watching.

GoRails

GoRails covers all kinds of Rails & Ruby subjects over an enormous library of movies. Its actually a fantastic place for freshmen preferring to be lead step-by-step via an issue to be taught.

RailsCasts

A traditional for many who received began a decade in the past and nonetheless surprisingly related at this time. The Professional movies have been not too long ago made free by the writer as a result of the movies are dated.

Extending Rails

As you start adding more features to your application, you will begin looking at plugins that add features to Rails apps like authentication, payment, and more. Fortunately there’s some really great resources out there to help you figure out which gem is most suitable for your project.

The Ruby Toolbox

After I do analysis to see what RubyGem I would like to put in in Rails to resolve an issue, the primary place I look as https://www.ruby-toolbox.com to see what different related libraries exist. This helps me make a extra knowledgeable choice about which library I ought to shut.

Awesome Ruby

Superior Ruby is a extra transient listing of the favored RubyGem’s that the Ruby and Rails neighborhood use of their purposes.

Newsletters

You don’t need to subscribe to all of them, but its worth picking one to read at least once a week so you can keep track of all the new stuff and changes that comes to Rails.

Ruby Weekly

Contains basic information & updates about Ruby & Rails, hyperlinks to new tutorials & articles, and a bit on code & instruments which are new or have been up to date.

Short Ruby News

A abstract of Tweets & Toots from folks engaged on attention-grabbing issues inside the Rails and Ruby neighborhood.

This Week in Rails

A weekly publication from the Rails crew that covers adjustments and new options that land in Rails. This information letter is extra narrowly targeted on adjustments to the Rails framework, so that you’d miss out on numerous different information about Ruby Gems, plugins, and attention-grabbing neighborhood tutorials for those who solely subscribe to this.

Mobile Apps

If you get to a point where you want to ship a mobile application for Rails, there’s a whole stack for that. The team behind Rails has also released libraries that make it possible to easier integrate a Rails application with mobile apps.

Turbo iOS & Android

The crew behind Rails ships a library that makes primary integration with Rails attainable. There’s nonetheless numerous gaps within the framework that you’re going to have to fill in, like authentication.

The Turbo Native Guy

Joe Masilotti has been constructing Turbo iOS purposes for years and writes about them on his web site. Loads of his content material offers some fairly nice solutions to the gaps left open within the Turbo Native frameworks. Joe has even launched his personal starter package referred to as Turbo Navigator that fills in numerous the gaps left open within the Turbo iOS undertaking.

See Also

Sharp Edges

Like anything, Rails has sharp edges that are worth knowing about when you start using it.

JavaScript, CSS, and Image Assets

I’ve written about the asset pipeline being complicated. The default importmaps pipeline is affordable for someone transport a brand new app, however the second you could use a JavaScript bundle the requires compilation — or a node bundle’s CSS recordsdata, pictures, or different belongings, you may discover that the trail ahead can grow to be overwhelming.

Deployments

There’s no one way to deploy a Rails application, which is a decision that a person new to Rails has to make. Fortunately the upcoming Rails 7.1 release will include a way to generate Dockerfiles, which give an ordinary configuration of the server you may deploy to manufacturing.

Higher but, the dockerfile-rails gem will be added to any Rails undertaking to robotically generate Dockerfiles that may be deployed to host like Fly.io that settle for Dockerfiles. This is how one can shortly generate a Dockerfile in your Rails app and deploy to manufacturing.

$ bundle add dockerfile-rails
$ bin/rails g dockerfile
$ brew add flyctl
$ flyctl launch
$ fly open

If that every one goes nicely you may see a working Rails app.

Hotwire

Hotwire makes it possible to build responsive & realtime web applications without a ton of JavaScript. The documentation at https://hotwired.dev/ was written extra for the front-end so it may very well be agnostic to Rails, which makes it tougher to comply with at instances.

Rails ships with some helpers, referred to as Turbo Frames and Turbo Stream, which are largely documented within the Github repo at https://github.com/hotwired/turbo-rails, however not but within the Rails guides.

“Too Much Magic”

If you fired up a Rails console via bin/console rails you’d find the following commands work as you’d expect

irb(main):001:0> 24.megabytes
=> 25165824
irb(main):002:0> 1.month.from_now
=> Fri, 07 Apr 2023 00:45:35.762342000 UTC +00:00

But if you ran the same thing in “Regular Ruby” via irb none of them would work. That’s because Rails adds methods to core Ruby classes that gives them super powers called Active Support. You may end up switching between these docs and Ruby’s docs.

This Is the Tip of the Iceberg

There’s so many amazing people in the Ruby and Rails community shipping incredible stuff. What’s really great about the Rails community is how many resources are available for everybody using Rails including folks just starting out all the way up to the most Sr Rails engineers.

The best thing you can do to get started is pick a problem you’d like to solve, then build a solution for it in Rails and see how far you can get with the resources above.



Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top