Commandes Rails
rails _rails.version_ new app_name --database=postgresql
rails generate model Post title:string likes:integer
rails generate controller Post
rails tmp:clear # delete tmp files
rails log:clear # delete logs
rails notes # search for TODO
rails dbconsole # database console
rails stats # thousands of lines of code and test ratio
rails about # information about your application's environment
rails db:reset # drop database + create tables found in schema.rb
rails routes --unused # unused routes
rails runner path/script.rb # exécuter un fichier
bundle audit # print gem with criticality security file
bundle outdated # print table with current and latest gem version
Active Support
Time.current.since(60 * 60)
# Same as
1.hour.from_now
Time.current.all_day # Range
Time.current.all_week # Range
Time.current.all_month # Range
Active Job (Rails documentation)
MyJob.perform_now
Active Storage (Rails documentation)
url_for(post.image)
Seed with active storage
require "open-uri"
file = URI.open("https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/NES-Console-Set.jpg/1200px-NES-Console-Set.jpg")
product = Product.new(title: "NES", body: "A great console")
product.picture.attach(io: file, filename: "nes.png", content_type: "image/png")
product.save
Hash#presence
params[:return_to].present? ? params[:return_to] : root_url
# SAME AS
params[:return_to].presence || root_url
Mail To
<% mail_to "alex@bouvier.fr",
"Nouvel email",
subject: "Hi, Alex!",
body: "J'adore ton blog" %>
Informations sur les gems (valable aussi pour tout projet avec un Gemfile)
bundle open activerecord # ouvrir le code source
bundle add activerecord # ajoute au Gemfile et ca fait le bundle install
bundle info activerecord # voir le path
bundle show activerecord # voir le path (deprecated)
bundle update rails # mettre à jour
String Inquirer (source)
class Order < ApplicationRecord
AUTHORIZED_CATEGORIES = ['api', 'default']
def category
super&.inquiry
end
end
order.category.api?
order.category.default?
Routing in multiples files
Rails.application.routes.draw do
draw :api
end
# config/routes/api.rb
namespace :api do
resources :orders
end
Afficher les query SQL dans les logs en production (source)
ActiveRecord::Base.logger.extend(
ActiveSupport::Logger.broadcast(ActiveSupport::Logger.new(STDOUT))
)
ActiveRecord::LogSubscriber.attach_to :active_record
CLI Scalingo (source)
git remote add scalingo git@ssh.osc-fr1.scalingo.com:app-name.git
scalingo run bundle exec rails console
Setup des meta data dans Rails
Comment faire des Services dans Rails
Une introduction à la gem ViewComponent
Tuto sur Turbo
How to use .nil? .empty? .blank? .present? in Rails 6
Mastering Eager Loading and Beyond! Rails 7 (includes, join, preload)
Drifintg Ruby Episode #387 - Demystifying the Asset Pipeline (video)
How to build skeleton screens for Ajax responses in Ruby On Rails with minimum JS
Active Storage: How it works
Notes on Performance Optimization in Rails Applications
Rails inspire: A curated collection of code samples from Ruby on Rails projects
First commits in a Ruby on Rails app
Rails n’est pas simple !
Rails Active Storage Cheatsheet